Unable to set headers with Async http network.request

I’m having trouble posting json data to a webservice using the ansynchronous http network.request call. Seems like the content-type of the body is always “application/x-www-form-urlencoded” regardless of how I try to set the header. I also tried setting the headers on a GET request with no luck.
The documentation on how to pass the parameters is a little ambiguous with regards to params/headers/body. Other than the headers the code below works.
I’m testing in the Corona simulator, build 268.
An http packet sniffer shows the same result as the test webservice.
A sinatra webservice to see what’s posted:
[ruby]
require ‘rubygems’
require ‘sinatra’
require ‘json’

post ‘/json’ do
puts “testing json post”
print "path_info: "; puts request.path_info
print "content_length: "; puts request.content_length
print "media_type: "; puts request.media_type
print "params.inspect: "; puts params.inspect
request.body.rewind # in case someone already read it
data = JSON.parse(request.body.read.to_s)
print "data.inspect: "; puts data.inspect
puts “done.”
“[html]Your response[/html]”
end
[/ruby]

The corona code that can post the body, but the headers don’t get set at all:
[lua]local json = require(“Json”)

local myText = display.newText("(Waiting…)", 0, 0, native.systemFont, 16)
myText.x = display.contentCenterX
myText.y = 120

local function networkListener( event )
if ( event.isError ) then
myText.text = “Network error.”
else
myText.text = “Done.”
print ( "Response: " … event.response )
end
end

local response, params, headers, body
local jsonData = {[“foo”] =“bar”}
body = Json.Encode(jsonData)

print (“jsonData=” … body)
print (“jsonData.length=” … string.len(body))

headers = {}
headers[“Content-Type”] = “application/json”
headers[“Content-Length”] = #body
headers[“From”] = “me@me.com
headers.body = body

params = {}
params.headers = headers
params.body = body

local localUrl = “http://localhost:4567/json
–network.request( localUrl, “POST”, networkListener, params ) – crashes
network.request( localUrl, “POST”, networkListener, headers ) --headers not set[/lua]

Output from sinatra for the above looks like this:
[text]
testing json post
path_info: /json
content_length: 13
media_type: application/x-www-form-urlencoded
params.inspect: {"{“foo”:“bar”}"=>nil}
data.inspect: {“foo”=>“bar”}
done.
[/text]
This curl script that works fine:

curl -H "Content-Type: application/json" -i -X POST "http://localhost:4567/json" -d '{"hi":"ho"}'  

Output from sinatra for this curl script:
[text]
testing json post
path_info: /json
content_length: 11
media_type: application/json
params.inspect: {}
data.inspect: {“hi”=>“ho”}
done.
[/text] [import]uid: 6786 topic_id: 6318 reply_id: 306318[/import]

Tested on the iphone 4 with the same result - just to rule out that it’s not the simulator. [import]uid: 6786 topic_id: 6318 reply_id: 22116[/import]

u can’t do local host on the device

c [import]uid: 24 topic_id: 6318 reply_id: 62523[/import]

Hi all,

I’m having the same problem using an actual valid server-url. if I’m doing it as above with the params…the request (POST) is not sent at all. If I’m doing as above with the headers directly the request is reaching the server but the headers were not set.

In general I’m using quite a lot of time searching for samples for the corona platform and there is but sometimes just not working…that’s a bit annoying. I’m doing a master in interaction design and really wanted to use Corona but every day there are stones in the way I can’t get rid off.

Would really be nice if someone could point the problem above out.

Thx! [import]uid: 72230 topic_id: 6318 reply_id: 70887[/import]

Hey plausch,

You’d be better off starting a new thread containing code along with more details.

If you need fast assistance in getting your project to work then I’d encourage you to make use of our premium support service. Link; http://www.anscamobile.com/corona/support/

Peach :slight_smile: [import]uid: 52491 topic_id: 6318 reply_id: 70955[/import]

Hi again,

I’m posting this here because this thread is in my opinion still open :wink:

Issue: I’m not able to set the headers in a post-request using “network.request”. Please see the tests I did. The headers always stayed “application/x-www-form-urlencoded”.

Question: What is the right way to do this?

I’m using the build 591 and testing on Windows 7.

Test 1

function UploadTestParams()  
  
 local headers = {}  
   
 headers["Content-Type"] = "multipart/text"  
 headers["Content-Length"] = 4;  
  
 local params = {}  
 params.headers = headers  
 params.body = "1234";  
   
 network.request( "http://www.myserver.com/UploadTest1.aspx", "POST", networkListener, params)  
  
end  

– Results
Content-Type: application/x-www-form-urlencoded

Test 2

function UploadTestHeaders()  
  
 local headers = {}  
   
 headers["Content-Type"] = "multipart/text"  
 headers["Content-Length"] = 4;  
 headers.body = "1234";  
   
 network.request( "http://www.myserver.com/UploadTest1.aspx", "POST", networkListener, headers)  
  
end  

– Results
Content-Type: application/x-www-form-urlencoded [import]uid: 72230 topic_id: 6318 reply_id: 71026[/import]