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

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.

Related

Take all data from Zapier Storage

I need to get all the data from StoreClient (Javascript). The format of the data on the below picture, they are in Zapier Storage.
const store = StoreClient('mysecret');
const value = await store.getMany('mykey'); // or store.get('mykey')
return {result: value}
This code works well. But I need to take and process all stored keys in a loop along with all their child value. I did not find a way :(
I tried store.list_pop(key), but the lists have a different storage format. And the data is not retrieved.
I would recommend using Zapier's storage API which will allow you to retrieve all stored data through a GET request to https://store.zapier.com/api/records. I often have to do the same thing and this works for me.
Have a look at their documentation here. I typically code in Python using the requests library. But I'm sure you could achieve similar results using an ajax or fetch request in Javascript.
EDIT
If I am understanding your question correctly you are trying to 'GET' all of your data stored in Zapier's storage client. As per their API documentation:
Zapier stores your data as a dictionary object which can hold key value pairs. Those values can also be nested dictionaries or lists. Regardless of how you store the data (simple key value pairs, nested lists, nested dictionaries, or some combination of the preceding) a 'GET' request will return the entire object. As stated before I typically use Python's request library to handle HTTP requests but I was able to achieve the same result using a Javascript fetch request. I setup a dummy storage account at https://store.zapier.com/api/records?secret=dog to test and illustrate how this works. See my code below.
var url = "https://store.zapier.com/api/records?secret=dog";
const res = await fetch(url);
const body = await res.json()
return {JSON : body}
Unfortunately, due to my lack of familiarity with Javascript I had to bake the secret into the url, which I don't think is ideal, but for the purposes of this example it does the job. See my output below.
As you can see the 'GET' request returned all data stored in my Zapier storage account. I simply returned the data I retrieved from the 'GET' request but you could of course loop through the results and execute logic as needed. This does not modify any of the data stored, what I often do is pull in all of my stored date with a 'GET' request, modify it, delete the old storage, and 'POST' my modified storage information. This allows me to limit my requests to two calls rather than modifying each individual value.

Order mismatch from RestAssured Response object to JSON conversion

I got a Rest Assured Response object. While trying to convert from Response object to JSON. The conversation happen successfully but JSON order is mismatch. It would be great if some one assist on this.
Conversion:
JSONObject jobj = new JSONObject(restAssuredResponse.getBody());
Basically, JSON is an unordered set of key value pairs. Which, for any JSON object the order of key/value pairs shouldn't be considered.
The JSON Object should be considered equal even if the order is not the same.

Save a large JSON to Realm using Swift 3

I have a JSON with more or less 75 keys.
I need to receive this JSON and store offline it using Realm.
I do not want to iterate through the keys, since I've heard that there are ways to save a large JSON using a few lines. How can I do this?
EDIT:
My JSON (
I saved on a server away because it's too big)
http://myjson.com/i7e6l
There is no easy, one liner to parse the JSON and store it in Realm, since each JSON response is unique and no framework can have explicit knowledge about the structure of your JSON response without you giving some information to this framework about your JSON.
You will need to write some code either to parse the response or to make a mapping between your JSON response's fields and the properties of your Realm object. If you choose the latter solution, you can use Alamofire Object Mapper to do the JSON parsing automatically, but even then you have to write code for the mapping.

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

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

Rearrange the order of keys in NSDictionary for crypto hash match of entire dictionary

Here is what I'm trying to achieve.
I have a payload in the below format.
//Ordered Payload - observed at the receiving end.
{
"xx":"First",
"yy":"Second",
"11":{
"xx1":"innerVal1",
"xx2":"innerVal2"
}
}
Since NSDictionary is basically unordered, it kinda shuffles this key - value pairs with in the dictionary when viewing on console of Xcode.
The same payload order is captured(as above ordered dict) when making a POST request at the api proxy end.
To ensure that the payload isn't tampered I need to do a hash(HMAC) of the payload and attach it in the request. The problem is that when I hash the payload (converting NSDict to JSON string) as is unordered just as NSDict property, and it won't match at the proxy when it tries to compares the hash with that of the ordered payload received in the request.
When I do a 'po' on NSDictionary this is what I see and its the same every time.
//unOrdered Payload - observed at the sending end.
//observe that the key holding a dictionary is always seen first in the dictionary followed by the other key-value pairs.
{
"11":{
"xx2":"innerVal2",
"xx1":"innerVal1"
},
"yy":"Second",
"xx":"First"
}
Is there any way I could make sure that my hash's are the same when the payload is not tampered ?
I understand its clearly not so possible, I think its worth giving a shot.
I have come up with other logic to use directly the key-value pairs for hashing and not the complete dictionary.
But since the payload format could be changing over the time, I don't want to keep changing the code at both client and server side for it.
Appreciate your time to help me find a solution for it.
Just hash the JSON data prior to sending, not the dictionary.
Convert the NSDictionary to JSON data with NSJSONSerialization method: dataWithJSONObject.
Hash the JSON data.
Sent the JSON data and hash.
On the server side:
Hash the received JSON data
Compare the computed hash to the received hash value
If hasing the JSON data is not an option just hash all the keys and values together or perhaps just the values if the keys are not an issue. Sort the keys and use that key order for hashing order.
Or just use SSL.

Resources