我一直在使用带有Python的套接字,而且我试图理解为什么应该在字段data1和data2上发送一些数据的POST不起作用。
POST /method.php HTTP/1.1\r\nHost: localhost\r\nContent-Type: multipart/form-data\r\n\r\ndata1=something&data2= otherthing\r\n\r\n
此请求有什么问题?
答案 0 :(得分:1)
您是否尝试过使用Requests库,以及
下面的帖子请求示例import requests
header = {"Content-Type": "multipart/form-data"}
data1="something"
data2= "otherthing"
session_requests = requests.session()
result = session_requests.post("http://localhost/", data=dict(data1, data2), headers=header)
答案 1 :(得分:1)
使用Online grok debugger库可能更容易,因此您的代码看起来像这样:
function NASearch(query, callback) {
var data2 = "{\r\n \"ApiKey\": " + ak2 + "\r\n\r\n}";
xhr33 = new XMLHttpRequest();
xhr33.withCredentials = true;
xhr33.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
if (typeof callback == "function") {
// apply() sets the meaning of "this" in the callback
callback.apply(xhr33);
}
//alert(OrgLegalName + "2")
}
});
xhr33.open("GET", "https://example.net/SmartSearch/NameAndAddressSearchAutoComplete?maxLength=31&clientAccountsOnly=True&accountPurposeTypeId=&institutionIdentityRecordId=&collectiveFundOnly=&query=" + query);
xhr33.setRequestHeader("accept", "application/json");
xhr33.setRequestHeader("x-api-key", ak2);
xhr33.setRequestHeader("authorization", "Basic " + cheetauth);
xhr33.setRequestHeader("cache-control", "no-cache");
xhr33.setRequestHeader("postman-token", "08c9199b-f6b4-22f1-bb8a-5d0dbdaffee0");
xhr33.send(data2);
}
NASearch(btn.value,
function() {
//while (theultheb.firstChild) theultheb.removeChild(theultheb.firstChild);
somevar3 = JSON.parse(xhr33.responseText).suggestions;
console.log(JSON.parse(xhr33.responseText));
// console.log(SFaccountresponse);
console.log(somevar3)
for (var i = 0; i < somevar3.length; i++) {
var identid = somevar3[i].data;
var identval = somevar3[i].value;
console.log(somevar3[i].data); //variable is defined here
$.ajax({
type: 'GET',
url: 'https://example.com/Organizations/Detail/' + somevar3[i].data,
success: function() {
console.log("EXISTS");
console.log(somevar3[i].data);
},
error: function() {
console.log("does not exist");
},
complete: function(xhr) {
var xhrresp = xhr.getResponseHeader('Content-Length');
if (xhrresp == 44) {
var link1b = document.createElement("a")
link1b.href = "https://example.com/Individuals/Detail/" + somevar3[i].data;
link1b.class = "k-link active";
link1b.appendChild(document.createTextNode(somevar3[i].value)); //undefined here
li1b = document.createElement("li");
li1b.class = "listyle ellipsis-overflow";
li1b.appendChild(link1b);
ul1b.appendChild(li1b);
} else {
var link1b = document.createElement("a")
link1b.href = "https://example.com/Organizations/Detail/" + somevar3[i].data; //undefined here
link1b.class = "k-link active";
link1b.appendChild(document.createTextNode(somevar3[i].value)); //undefined here
li1b = document.createElement("li");
li1b.class = "listyle ellipsis-overflow";
li1b.appendChild(link1b);
ul1b.appendChild(li1b);
}
}
});
}
}
);
您还可以requests和send files
答案 2 :(得分:1)
您的请求有几个问题:
POST /method.php HTTP/1.1
Host: localhost
Content-Type: multipart/form-data
data1=something&data2= otherthing
首先,每当在HTTP请求中使用正文时,必须知道正文的长度。这通常通过在HTTP标头中预先设置Content-length
的长度来完成,尽管如果事先不知道全长,也可以使用分块编码。您的请求不会执行任何这些操作,这意味着请求是无效的HTTP请求。
此外,您声称Content-Type
为multipart/form-data
,但您的身体不属于此类型。使用multipart/form-data
,您的正文将包含由文本边界分隔的多个MIME部分,并且此边界需要在Content-type
标头中声明。您显示的正文的正确类型将是application/x-www-form-urlencoded
。
即使application/x-www-form-urlencoded
身体部分错误。这种类型的主体应该只是由key=value
连接的&
对,即在data2=
之后的密钥后面既不应该是空格,也不应该在body = "data1=something&data2=otherthing"
request = ("POST /method.php HTTP/1.1\r\n" + \
"Host: localhost\r\n" + \
"Content-Type: application/x-www-form-urlencoded\r\n" + \
"Content-Length: %d\r\n" + \
"\r\n%s") % (len(body),body)
之后添加新的行。你有数据的结尾。
当删除所有这些问题时,您应该发送以下请求:
pm.environment.unset(variableName)
但是一旦你发送了这个请求,麻烦就会继续,因为正确地获得响应也很复杂。通常我建议不要编写自己的HTTP处理代码,除非你真的知道你做了什么,而是使用现有的库。虽然在查看一些示例请求时,HTTP可能看起来很简单,但它比最初看起来更复杂。虽然您的代码可能似乎对特定服务器起作用,但它可能会因其他服务器而失败。