IOS/Objective-C/JSON:Obtain single value from Web Service in JSON - ios

I want to obtain a single value from a web service in JSON, just a file name i.e. "picture.png"; based on some parameters passed.
Can IOS (I guess NSJSONSerialization JSONObjectWithData:) handle this single value in JSON or on the server side should I have it send a dictionary key as in {"pic": "picture.gif"}
If there is no picture, I am returning "nopic" so again should I have it return "error" or {"error": "nopic"}
I gather the various JSON specifications are conflicting on this point so my interest is just practical...how best to handle this case.
Thanks for any guidance on this

Related

How does `Alamofire.Session.upload(multipartFormData:to:method:headers:)` incorporate the payload into the request?

I'm creating a REST API from scratch for a preexisting iOS app whose API source code is, for all intents and purposes, no longer available. This means I'm analyzing how each endpoint expects requests sent to it to be formatted. One of those endpoints is communicated with via Alamofire's Session.upload(multipartFormData:to:method:headers:).
The multipartFormData is a closure parameter that is passed a MultipartFormData object, permits the consumer to mutate it (add key-value pairs to it in this case), and then return it from the closure.
My question is how does that addition of these key-value pairs to the MultipartFormData affect the format of the HTTP request. Up until this point every .post method has been passed a Dictionary<String, Any> and JSON has been specified as the encoding, so it has been straightforward to assess how the payload is uploaded (a single JSON object with a parameter for each entry in the dictionary). But it is not so straightforward with this Session.upload(multipartFormData:to:method:headers:) method. I essentially want to know how the key-value pairs are encoded into the request so I know how to decode them server side.

Issue with nested array in Zapier Trigger Response

Hi I'm having issues building my Zap with SignNow. I'm trying to extract the email of the Signer from the response data, but because the Signers portion of the response data is a nested array Zapier appears to just flatten the values into the key Name.
So instead of getting a consistent Key named 'Signers' to reference I instead get key = 'Signers FIRSTNAME LASTNAME' which varies each instance of the zap and causes it to break.
I only need the email that is stored in this section to be able to lookup their account via our CRM but can't get it to run consistently because of this key naming issue. Is there some way I can parse through the raw response data of this trigger?
Screenshot of Response Array
Screenshot of Zapier Object with Flattened Key Name

How to use "q" Query parameter in Bitbucket REST API?

So I am aware of the fact that you can get the files in a repository by using
https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}
API.
The API also supports the q parameter where you can query the resulting json object based on various fields in it.
I wanted to know if one can use this query parameter to fetch the files based on certain extensions?
Like the JSON object also contains a field "mimetype" which defines the mime type of the files.
I did use the following API call
https://api.bitbucket.org/2.0/repositories/{workspace}/openapi/src/master/?max_depth=100&q=path+%7E+%22.js%22&pagelen=100
To fetch all the files which contain the string ".js" in the path parameter.
But while querying the mimetype parameter I was not able to do the same using
https://api.bitbucket.org/2.0/repositories/{workspace}/openapi/src/master/?max_depth=100&q=mimetype+%3D+%22application%2Fjavascript%22&pagelen=100
This call returned error.
Can anybody let me know how can you query based on mimetype or if possible fetch the files based on extension
Note: I know about the Code search API but that is not an option as it has large limitations.

Cloud Code Parameters and Results

I'm making an iOS app using swift and am using a cloud backend for the first time, namely Parse.com. I am curious what I can pass as parameters to cloud code functions and then receive as a return value. I want to send an array of PFObjects and also return an array of them, but I get the error Caught "NSInternalInconsistencyException" with reason "PFObjects are not allowed here." I am thinking of instead sending an array of dictionaries of the values I need to send and then return the same way. Can I do it this way, or is there another, better way to do this?

REST call may results in two different JSON objects. What design pattern should I use?

My web application makes a REST call. If the call is successful, it will return a 'weather' json object. If the call fails, it will return a json error object.
I want to make a class that parses the resulting JSON and returns a Weather object if the call succeeded and an Error Object if the call failed.
I'm thinking of using the Factory pattern but I'm not sure if that's a good approach because the two objects are very different from one another. What is a good way to design this code?
A common approach I use is to have Weather and Error both be Response objects and have a ResponseFactory create them.
I strongly encourage you to use proper HTTP codes when designing your service as they give a more general view of the state and success of each call.
You need first to check the result of the call, and then make a decision on how to handle it, with the possibility of handling all error codes with an error callback that returns an Error JSON object, and a success callback to return a Weather JSON object. You can use the HTTP codes to create a proper response and further subdivide the logic to return more specific errors, if needed.
The use of a Factory pattern seems overkill, specially given that the objects don't relate to each other.
It really depends on the environment you'll be using your API.
As a rule of thumb, rely on the HTTP code - if you get a 404 or a 500 of course you can't come up with a parsed response.
Format your error responses in a consistent way, e.g.
404 { "message" : "Resource not found" }
400 { "message" : "Wrong parameters given" }
So you know how to parse them.
If you get a 200 OKyou know everything was right, and you can parse your response with no problem at all.
Does the Content-Type header vary depending on the type of response?
As some have noted in their answers, the HTTP status code should be used to determine "Was there an error", but just as important is the interpretation of the content type returned.
Hoping the Content-Type header does vary, I would suggest using a registry of parsers, registered by content-type they handle, and then delegate to them to handle understanding how to convert a particular content type into the object you want. In Ruby, since you didn't specify a particular language:
case response.status:
when 200..299
return parsers[response.content_type].parse(response.body)
when 400..499
raise parsers[response.content_type].parse(response.body)
else
raise "Unhandled response status"
Doing so separates the two concerns:
Determining if there was an error
Parsing of content types into classes/types in your application.

Resources