0

I am trying to POST JSON content to a web service. It works fine from POSTER (firefox plugin) and from HTTP Client (Mac) but throws 500-Internal Server Error when trying to POST from XCode (iOS App). Can someone help me out with this.

The request details are :-

HTTP Method : POST

Content-Type : application/json

HTTP Body :

{
  "HasSunRoof" : "true",
  "HasTiltWheel" : "true",
  "VIN" : "XXXXXXXXXXXXXXX",
  "VoluntarySignatureComments" : "Voluntary sign comments",
  "Make" : "Audi",
  "IsVinManuallyEntered" : "false",
  "AssociatedCompanyId" : "2",
  "ReposessionMethod" : "Voluntary with release",
  "HasKeys" : "true",
  "CreatedBy" : "RAMBO",
  "InteriorCondition" : "Poor",
  "HasPowerWindows" : "true",
  "GlassCondition" : "Good",
  "TransmissionType" : "Auto",
  "EngineType" : "EngineType1",
  "RecoveredFromAddress1" : "Myaddress1",
  "CompanyId" : "1",
  "HasPowerSeats" : "true",
  "HasAirConditioning" : "true",
  "Mileage" : "1243",
  "HasCruiseControl" : "true",
  "Trim" : "Trim data",
  "HasRadio" : "true",
  "HasSpares" : "true",
  "FuelType" : "Alternative Fuel",
  "IsDriveReady" : "true",
  "TireCondition" : "Average",
  "IsActive" : "false",
  "CreatedOn" : "5\/24\/12",
  "RecoveredFromAddress2" : "Myaddress2",
  "IsRunnable" : "true",
  "RecoveredFromState" : "Mystate",
  "BodyStyle" : "Style1",
  "RecoveredFromZip" : "90009",
  "IsVoluntary" : "false",
  "InteriorConditionDescription" : "Poor interiors",
  "RecoveredFromCity" : "Mycity",
  "Model" : "A6",
  "Year" : "2009"
}

EDIT : Solved See below for the fix to my noobish bug.

ilight
  • 1,622
  • 2
  • 22
  • 44

2 Answers2

1

I could successfully send the request now. It was bad on my part that I built a HTTP request the wrong way.

Solution: Use setValue:forHTTPHeaderField: and NOT setValue:forKey: for the http headers like "Content-Type","Content-Length" etc.

I was thinking as long as the key is unique, it did not matter whether we use any of them. But I was wrong.

ilight
  • 1,622
  • 2
  • 22
  • 44
0

Its very difficult to answer your question unless you post your actual code.

However check following things.

  1. You have to use MSMutableRequest and set HTTPMethod as @"POST"
  2. Make sure you set header Content-Type to "Application/json" (you have mentioned you did this already)
  3. Make sure you set Content-Length to the string length of the jsonstring.
  4. Also i am not sure (strings)"true" and "false" are supported by json, it should be set to [NSNumber initWithBool:YES/NO], Alternatively you can use SBJson Writer to properly encode to json string.

Best is to see why your server is sending that error. You can check your server logs.

suresh
  • 2,365
  • 1
  • 26
  • 36
  • "true"/"false" are being accepted. As I mentioned, the request works on everything except from XCode. – ilight May 24 '12 at 07:09