Why I cannot send multipartFormData with .POST parameters in Alamofire? - ios

When I'm trying to send this:
Alamofire.upload(
.POST,
URL,
parameters: parameters,
multipartFormData: { multipartFormData in
it prints me the next error:
Type of expression is ambiguous without more context
I want to send my image file with the next parameters:
{
"user": {
"firstName": "ABC",
"email": null,
"birthDate": "1988-01-03",
"books": [{
"id": 1
}, {
"id": 2
}]
},
"locale": "en",
"sign": "It's me",
}
because of this structure, I cannot append it to multipart and I do not need send my parameters as a body of multipart, but as a parameters.
Is there any solutions for it?

Related

Microsoft Graph - Education user batch request not returning a value property

Running batch requests against most Microsoft Graph API endpoints returns a object with a value property like this:
{
"id": "2",
"status": 200,
"headers": {
"Cache-Control": "no-cache",
"x-ms-resource-unit": "1",
"OData-Version": "4.0",
"Content-Type": "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
},
"body": {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(id,displayName,student,teacher)",
"value": [
{
"id": "ebe6297c-1f76-484e-9616-5e8e6be6098e",
"displayName": "Adele Vance"
}
]
}
},
However running a batch request against /education/user/{userId} gives me:
{
"id": "1",
"status": 200,
"headers": {
"Cache-Control": "no-cache",
"OData-Version": "4.0",
"Content-Type": "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
},
"body": {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#education/users(id,displayName,student,teacher)/$entity",
"id": "ebe6297c-1f76-484e-9616-5e8e6be6098e",
"displayName": "Adele Vance"
}
}
Without any value property.
This looks like a bug. Not sure how to proceed.
Best regards,
Oskar
It depends on the endpoint you are calling in batch request.
For endpoints returning a collection, the batch response contains "value": [{..},..] in body...collection of objects.
For endpoints (like your endpoint GET /education/users/{id} that return single object, the batch response contains the object.

how to define nested dictionary for alamofire POST request

Hi I have a nested dictionary that I need to pass to a function to make a POST request using alamofire.
this is the function that conduct the API call
func apiCall(productParameter: [String:String]) {
AF.request(dataURL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
debugPrint(response)
//some other code here.
}
}
in my viewController, I need to call this function:
apiCall(productParameter: [String:String])
the problem is I have a very complicated nested dictionary to pass... like this :
{
"userId": "string",
"price": {
"currency": "USD",
"amount": 99.96
},
"paymentModel": "string",
"description": "string",
"product": [
{
"description": "string",
"name": "string",
"images": [
"string"
]
}
]
}
do you know how to define the function and data type? thanks.
You should try [String : Any] in this case, so that your keys are always Strings but the values can be Strings, Dictionarys, Arrays and so on.

Get contacts by IDs in batch in Microsoft Graph

I have a list of IDs which I want to pull the corresponding contacts from Microsoft Graph. I tried filtering with https://graph.microsoft.com/v1.0/me/contacts?$filter=Id eq 'my-id-here' or Id eq 'other-id-here' but it says
ErrorInvalidProperty The property 'Id' does not support filtering.
I know than I can query by calling https://graph.microsoft.com/v1.0/me/contacts/my-id-here but I want to request multiple at once to minimize round trips.
You can use batch processing to execute multiple requests. For each request you need to provide a request id, a http method and a request url. Sample request:
URL: https://graph.microsoft.com/v1.0/$batch
Http Method: POST
Request Body:
{
"requests": [
{
"id": "1",
"method": "GET",
"url": "/me/contacts/firstId"
},
{
"id": "2",
"method": "GET",
"url": "/me/contacts/secondId"
}
]
}
Graph will send a response such as:
{
"responses": [
{
"id": "1",
"status": 200,
"headers": {
...
},
"body": {
...
}
},
{
"id": "2",
"status": 200,
"headers": {
...
},
"body": {
...
}
}
]
}
Make sure, that you check for the id of the request, since requests may not be returned in order, in which you sent them to Graph. The Microsoft documentation on batch processing provides more information: here.

Microsoft graph batch inserting new task and updating details

Current steps for creating task are
POST /planner/tasks
GET /planner/tasks/{id from post call}/details
PATCH /planner/tasks/{id from post call}/details
If-Match: {etag from get call}
but I want to batch three steps in single call using https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching
And according to odata v4 references http://docs.oasis-open.org/odata/odata-json-format/v4.01/csprd02/odata-json-format-v4.01-csprd02.html#sec_ReferencingNewEntities we can refer entities in same batch call using ${id of other request}
{
"requests": [
{
"id": "task",
"url": "/planner/tasks",
"body": {
"title": "asff",
"appliedCategories": {
"category5": true
},
"planId": "mSV7ODf3g0iTJrUtsNcvHZYAB-ZW",
"bucketId": "WFN6kxMykE-4xxqLUh1uS5YALCWq",
"assignments": {
"4393baf8-8a52-4164-bf93-b1cba5130329": {
"#odata.type": "#microsoft.graph.plannerAssignment",
"orderHint": " !"
}
},
"dueDateTime": "2018-04-23T18:30:00.000Z"
},
"method": "POST",
"headers": {
"Content-Type": "application/json"
}
},
{
"id": "getDetail",
"method": "GET",
"dependsOn": [
"task"
],
"url": "/planner/tasks/$task/details"
},
{
"id": "patchDetail",
"dependsOn": [
"getDetail"
],
"url": "/planner/tasks/$task/details",
"method": "PATCH",
"headers": {
"Content-Type": "application/json",
"if-match": "$getDetail"
},
"body": {
"description": "gwrthbetrhnety"
}
}
}
]
}
but Get details call is failing with error
{
"id": "getDetail",
"status": 400,
"body": {
"error": {
"code": "BadRequest",
"message": "The request URI is not valid. Since the segment 'tasks' refers to a collection, this must be the last segment in the request URI or it must be followed by an function or action that can be bound to it otherwise all intermediate segments must refer to a single resource.",
"innerError": {
"request-id": "a46ce528-993f-4cff-865e-98b2b98d5f23",
"date": "2018-04-17T10:38:29"
}
}
}
}
What I'm doing wrong here
I believe what is missing is the URI (should be POST https://graph.microsoft.com/v1.0/$batch instead of POST /planner/tasks) and "id" field has to be numeric. Check out MS documentation:
https://learn.microsoft.com/en-us/graph/json-batching?context=graph%2Fapi%2F1.0&view=graph-rest-1.0
Here is an example of combining POST and GET in a batch call

Create a dictionary in swift with alamofire

Im trying to create a web request using alamofire in swift , by request object should be like this
{
"warranty": 0,
"descriptions": "string",
"product_name": "string",
"purchase_date": "23/10/2016",
"product_image": "string",
"product_receipt": "string",
"serial_number": "string",
"barcode_image": "string",
"serial_number_image": "string",
"product": {
"id": 1
},
"user": {
"id": 12
}
}
So in order to get this i have put my code as this
let parameters :[String:AnyObject] = [
"warranty":product.warrenty,
"descriptions":product.longDescription,
"product_name":product.initialName,
"purchase_date":product.purchaseDate,
"serial_number":product.serialCode,
"product": [
"id":product.id
],
"user": [
"id":userDefaults.getCustomerId()
]
]
But i when i do the request it seems that server doesn't accept this format , probably the way i assign
"product": {
"id": 1
},
"user": {
"id": 12
}
is not correct , what is the issue in here ? can some one point me the issue that i do here
I had the same issue and the problem was, i was missing the encoding option which you need to set .JSON. Match the below line with yours.
Alamofire.request(.POST, strURL, parameters: parameter as? [String : AnyObject], encoding: .JSON).responseJSON { (response: Response<AnyObject, NSError>) in

Resources