Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: post 方法,Content-Type逻辑判断有疑问!!! #23

Open
fanlushuai opened this issue Jan 7, 2021 · 12 comments
Open

Bug: post 方法,Content-Type逻辑判断有疑问!!! #23

fanlushuai opened this issue Jan 7, 2021 · 12 comments

Comments

@fanlushuai
Copy link

image

意思是,设置了。进行覆盖。没设置,不管了?

是不是应该是,没设置进行设置?

@asmcos
Copy link
Owner

asmcos commented Jan 14, 2021

后面有代码:req.Header.Set("Content-Type", w.FormDataContentType()) 会设置。

@fanlushuai
Copy link
Author

后面有代码:req.Header.Set("Content-Type", w.FormDataContentType()) 会设置。

你检查下吧。我抓包分析的,并没有带有。

刚才特意看了下代码。应该是有问题的。会被这个文件检查条件,干掉。

image

@fanlushuai
Copy link
Author

Content-Type", "application/x-www-form-urlencoded。顺便说下,是这个头

@asmcos
Copy link
Owner

asmcos commented Jan 15, 2021

你把你的测试代码发一下,我看看。

@fanlushuai
Copy link
Author

Snipaste_2021-01-16_05-54-58

image

func GetKillInitInfo(skuId string, num string) (initData InitData, err error) {
	url := "https://marathon.jd.com/seckillnew/orderService/pc/init.action"
	header := requests.Header{
		"Host": "marathon.jd.com",
	}

	data := requests.Datas{
		"sku":             skuId,
		"num":             num,
		"isModifyAddress": "false",
	}

// 两个图的差异,就是因为我在外面特殊的处理了一下。默认是没有这个头的,所以认为有问题!!!
	header["Content-Type"] = "application/x-www-form-urlencoded"
	resp, err := sessionReq.Post(url, header, data)
	if err != nil {
		fmt.Println("fuck initinfo 获取失败了。好好思考一下")
		return InitData{}, errors.New("请求错误")
	}

	var initdata InitData
	resp.Json(&initdata)
	if len(initData.AddressList) == 0 {
		return InitData{}, errors.New("响应错误,估计被频率限制了")
	}

	return initdata, nil
}

https://github.com/fanlushuai/go-jd-assistant/blob/c9f6d3fb66f1e2382662a975228f44704fb1017b/jdsdk/jdsdk_test.go#L109

@asmcos
Copy link
Owner

asmcos commented Jan 18, 2021

好的,我测试出来了,我试着改改

@fanlushuai
Copy link
Author

好的,我测试出来了,我试着改改

哈哈。

image

@asmcos
Copy link
Owner

asmcos commented Jan 18, 2021

我把if 判断去掉了。不过这样多次调用,他会不会重复加。你试试吧。

@asmcos
Copy link
Owner

asmcos commented Jan 18, 2021

419行,有一个问题,强制设置的话,别人就不能自己改了。这不好。

@fanlushuai
Copy link
Author

419行,有一个问题,强制设置的话,别人就不能自己改了。这不好。

你看后面,在解析外部传来的header的时候,会重新设置header。用户的传参如果设置了会直接覆盖这个默认逻辑。也就是说。用户的设置相当于优先级最高。

@fanlushuai
Copy link
Author

我把if 判断去掉了。不过这样多次调用,他会不会重复加。你试试吧。

单纯去掉if。应该是不行的。

因为我看到。header的数据结构为:type Header map[string][]string
再看下,add,set,get,的逻辑。
set会去覆盖,添加新值
add。是去给一个key追加一个新的元素。
而get是获取这个key的元素列表的第一个。

所以,set是合适的。add不合适。因为一直add不是我们的想法。也会造成内存问题。

@asmcos
Copy link
Owner

asmcos commented Mar 20, 2021

我现在的做法是 强制设置了 content-type,如果你想改变每次都加在header里设置。
如果有问题欢迎再次沟通。谢谢你的宝贵意见。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants