POSTing a JSON string gives an error #8

Closed
bryanoltman opened this Issue Jun 8, 2012 · 5 comments

Projects

None yet

2 participants

@bryanoltman

I'm trying to send a signed POST request with a single JSON string (see below) as a POST body. However, when creating the MKNetworkOperation, I receive a [JKDictionary urlEncodedString]: unrecognized selector sent to instance on line 164 of RSOAuthEngine.m.

If it helps my JSON looks like:

{
  "container": {
    "id": 781132,
    "status": "done"
  }
}

Let me know if there's any extra information I can provide.

@rsieiro
Owner
rsieiro commented Jun 8, 2012

RSOAuthEngine expects all POST values to be instances of NSString. It appears that request.readonlyPostDictionary is returning one object of the type JKDictionary, and this object doesn't support the urlEncodedString method (because this method is only added to NSString through a category).

I believe MKNetworkKit keeps the JSON body as an JKDictionary instance (this is from JSONKit, I believe) instead of NSString. I'll check this later, but for now you can convert it to NSString and it should work.

@bryanoltman

The problem I run into when converting it to a string is that the service I'm calling expects the POST body to be only JSON with no key ("{"entry":{"author_id":"7487348734","author_name":"test","coordinate": {"lat":38.399999999999999,"lng":-102.5},"comment":"testing","rating":5}}" instead of "key={"entry":{"author_id":"7487348734","author_name":"test","coordinate": {"lat":38.399999999999999,"lng":-102.5},"comment":"testing","rating":5}}"). It seems that, in order to make this work with an MKNetworkOperation, I would need to pass in a keyless params dictionary, which isn't possible as dictionaries can't have nil keys. What should I do to make this work?

@rsieiro
Owner
rsieiro commented Jun 8, 2012

I never tried myself, but I'm pretty sure MKNetworkKit should have a way to deal with JSON in the POST body. You should check with Mugunth (MKNetworkKit's author).

After you're able to POST the JSON you want, you need to check in your service's API documentation if the JSON body should be present in the OAuth signature (by default, every parameter in the POST body should be used in the OAuth signature, although I have no idea how you would append the JSON to the signature string).

Is the service you're using public? Can you point me to some kind of documentation?

@bryanoltman

It turns out that the POST data was not required as part of the signature, so by simply commenting out lines 162-166 of RSOAuthEngine.m (and making the default MKNetworkOperation postDataEncoding JSON), I got this to work. Thanks for your help!

@bryanoltman bryanoltman closed this Jun 8, 2012
@rsieiro
Owner
rsieiro commented Jun 8, 2012

I pushed a small fix that checks if the POST values are instances of NSString before adding to the OAuth signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment