I try to make http.request work. I want to send a post_data with it, but I don’t know the correct format.
local function http_result(self, _, response)
print(response.status)
print(response.response)
pprint(response.headers)
end
function init(self)
local postdata = "{'val1':22.34}"
http.request("http://examplepage.org/service","POST",http_result,nil, postdata)
end
results in
{“status”:{“apiCode”:“This value should not be blank.”,“apiMessage”:"([val1])",“httpCode”:400,“httpMessage”:“Bad Request”}}
no matter what I use in “post_data” string. I tried several inputs like “:” instead of “=” and several Brackets []{}. The reference page only refers to “post_data” as a string.
What is the correct format of “post_data” in http.request or am I doing something else wrong?
The correct format depends entirely on the service you are communicating with.
Where is the api specification?
Also, it mentions: "“apiCode”:“This value should not be blank.”. COuld that be the error? That you don’t specify the apiCode?
If I use for example an online request service (for example: https://www.codepunker.com/tools/http-requests) and send
lat=37.811784337568625&lng=-122.26420640945436&distance=3.125&myTracks=false&filterUserNames=false
I get:
{
“status”: {
“apiCode”: “600”,
“apiMessage”: " The request has been processed without incidents ",
“httpCode”: 200,
“httpMessage”: “Success”
…
Why can I get there a response without any api specification?
PS.: I hope I’m allowed to post external websites
Sorry if it is somewhat confusing. I know my api. In the first post it was “local postdata” variable as an example. It also works outside of Defold. Here is the request with curl:
curl --data “lat=37.811784337568625&lng=-122
.26420640945436&distance=3.125&myTracks=false&filterUserNames=false” http://open streetcam.org/nearby-tracks
When i put it in the Defold http.request function it does not work.
local function http_result(self, _, response)
print(response.status)
print(response.response)
pprint(response.headers)
end
function init(self)
http.request("http://openstreetcam.org/nearby-tracks","POST",http_result,nil, "lat=37.811784337568625&lng=-122.26420640945436&distance=3.125&myTracks=false&filterUserNames=false")
end
What am I doing wrong with the http.request function? And again I’m sorry if i missunderstand the main concept of this function.
If you run curl with -v for verbose mode you see headers as well:
$ curl -v --data "lat=37.811784337568625&lng=-122.26420640945436&distance=3.125&myTracks=false&filterUserNames=false" http://openstreetcam.org/nearby-tracks
* Trying 78.47.202.71...
* TCP_NODELAY set
* Connected to openstreetcam.org (78.47.202.71) port 80 (#0)
> POST /nearby-tracks HTTP/1.1
> Host: openstreetcam.org
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 98
> Content-Type: application/x-www-form-urlencoded
You need to pass the same content type header with Defold for the request to work:
local function http_result(self, _, response)
print(response.status)
print(response.response)
pprint(response.headers)
end
local headers = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
http.request("http://openstreetcam.org/nearby-tracks","POST",http_result,headers, "lat=37.811784337568625&lng=-122.26420640945436&distance=3.125&myTracks=false&filterUserNames=false")