Multipartform data not passing in all params to server-Swift 3 - swift

My requirement is to upload a recorded audio file with a file name and duration to a server using REST API
The code I have to form the multipart form data is as below
private func createRequestBody(boundary:"--BXDFG", multidata:Data, mime:"audio/mp4", filePathKey:"file", filename:"test", metas:["title":"Mytitle","duration":"30"] -> Data {
var body = Data()
body.append(Data("--\(boundary)\r\n".utf8))
body.append(Data("Content-Disposition: form-data; name=\"\(filePathKey)\"; filename=\"\(filename)\"\r\n".utf8))
body.append(Data("Content-Type: \(mime)\r\n\r\n".utf8))
body.append(multidata)
body.append(Data("\r\n".utf8))
if (metas != nil) {
for (key, value) in metas! {
body.append(Data("--\(boundary)\r\n".utf8))
body.append(Data("Content-Disposition: form-data; name=\"\(key)\"\r\n".utf8))
body.append(Data((value as! String).utf8))
body.append(Data("\r\n".utf8))
}
}
body.append(Data("--\(boundary)--\r\n".utf8))
return body
}
What I am doing is passing the audio as NSDATA and in a for loop I am adding "title" and "duration" to the multipart form request
On server side, I have below code
#api.route('/upload/<userid>', methods=['POST'])
def upload(userid):
if request.method == 'POST':
if 'title' not in request.form:
return jsonify({'error': 'Need a valid title for uploaded file', 'code': 403})
if "title" is not passed in multipart form data then the server will return an error response.
In my case, even though, "title" is added as a part of the request, the data is not passed to the server and the server returns an error. If I just pass the audio data alone, removing the validation for "title" on server side, then the audio data is uploaded successfully to the server.
I am not able to understand why "title" and "data" are not passed to server
PS: The API request works fine in POSTMAN client.
Thanks

This solved my case
Replacing line
body.append(Data("Content-Disposition: form-data; name=\"\(key)\"\r\n".utf8))
to
body.append(Data("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".utf8))
Added and additional /r/n

Related

How to connect to Webservice REST with POST from Power BI

I am trying to connect to a webservice through Power BI but I still do not achieve result, first try to use the Web data source and with the Advanced Use add the Header that in my case is Content-Type and its value is application/json and additional as Body I have a token
Where I get as a result the following:
Additional also try to use as source "Blank Query", where I accessed the advanced editor section and add the following Query:
I get as an error the following:
To make sure that the Webservice works correctly and obtains a result I have used the Advanced REST Client tool and I have made the following configuration:
Where you can see that the Headers section I have added the Header name Content-Type and the value of the Header Value is application/json, in the Body section is where I have added the token
With this I realize that my Webservice gets an answer and that the service is working correctly, I would like someone to give me a little guidance in a short time to perform correctly
Supply the content to switch the method from GET to POST eg
Perform a POST against a URL, passing a binary JSON payload and
parsing the response as JSON.
https://learn.microsoft.com/en-us/powerquery-m/web-contents#example-2
let
url = "https://postman-echo.com/post",
headers = [#"Content-Type" = "application/json"],
postData = Json.FromValue([token = "abcdef"]),
response = Web.Contents(
url,
[
Headers = headers,
Content = postData
]
),
jsonResponse = Json.Document(response),
json = jsonResponse[json]
in
json
or
let
url = "https://postman-echo.com/post",
headers = [#"Content-Type" = "application/json"],
postData = Text.ToBinary("{ ""token"":""abcdef""}"),
response = Web.Contents(
url,
[
Headers = headers,
Content = postData
]
),
jsonResponse = Json.Document(response),
json = jsonResponse[json]
in
json

Flutter Dio multipart image, Cannot read property '0' of undefined

I am trying to send multiple images to the backend server.
I have tried Dio and Http but the server responds with an error and the rest of the data except the images get saved.
The documentation says that the key values must be 'images_0' 'images_1' and so on.
Code -
The images are converted from XFile to multipart file.
int imgNum = 0;
for (int i = 0; i < data.images.length; i++) {
if (data.images[i] != null) {
File file = File(data.images[i]!.path);
String fileName = file.path.split('/').last;
http.MultipartFile mFile = http.MultipartFile(
'images_$imgNum',
file.readAsBytes().asStream(),
await file.length(),
filename: fileName,
);
imgNum++;
request.files.add(mFile);
}
}
(data.images is a list of XFiles? , request is the http.MultipartRequest)
All the other fields and headers are working and being saved correctly.
I have tried using Dio too which gives the same response. The response from the server is :
{data: {error: Cannot read property '0' of undefined}, message: Something went wrong. Please try again later.}
Our project also has a website where the form is working correctly, and when the http request there shows the images as follows : (in Google Chrome)
Under form data
Form Data source
The api team has also shared Swagger Ui API docs-
Docs
What am I doing wrong? How can I fix this?
Thank you for responding.

Relay Modern BadRequestError: Missing multipart field ‘operations’

I am trying to upload file to my server. using Altair i can do it without any error but when i use Relay.js for uploading it server throws following error.
BadRequestError: Missing multipart field ‘operations’ (https://github.com/jaydenseric/graphql-multipart-request-spec).
at Busboy.<anonymous> (/home/dotsinspace/Documents/dev/truck.pe__server/node_modules/.pnpm/graphql-upload#9.0.0_graphql#15.3.0/node_modules/graphql-upload/processRequest.js:362:11)
at Object.onceWrapper (events.js:420:28)
at Busboy.emit (events.js:326:22)
at Busboy.EventEmitter.emit (domain.js:486:12)
at Busboy.emit (/home/dotsinspace/Documents/dev/truck.pe__server/node_modules/.pnpm/busboy#0.3.1/node_modules/busboy/lib/main.js:37:33)
at /home/dotsinspace/Documents/dev/truck.pe__server/node_modules/.pnpm/busboy#0.3.1/node_modules/busboy/lib/types/multipart.js:52:13
at processTicksAndRejections (internal/process/task_queues.js:75:11)
Following are my graphql code and mutation which i am trying to commit.
#Graphql
graphql`
mutation AccountUploadMutation($profilePicture: Image!) {
AccountUpload(profilePicture: $profilePicture) {
message,
status
}
}
`
#Mutation
commitMutation(Environment, {
'mutation': AccountUploadMutation,
'variables': { 'profilePicture': v },
'uploadables': { 'file': v },
'onCompleted': (response, error) => Response({}, { response, error })
})
and I am totally confused about uploading part to..in uploadables you have to provide file..but my server looks for variable with profilePicture as image how can i deal with it.
It looks like you have an issue the multipart parsing configuration in your backend.
My guess is that the Relay Network is sending your GraphQL query in the mutlipart field "operation", but your backend is looking for the field "operations" (plural). To fix the error, confirm that your Network is sending the query in the operations field, or change your backend to read whatever field it's actually being sent on.
Another possibility is you're not sending your query in the multipart format at all. If you followed the Network documentation's example for sending your request, then you are just sending a JSON object, not a multipart form:
// Example from Relay docs. Sends a JSON object, not a multipart
// form as expected by your backend
function fetchQuery(
operation,
variables,
cacheConfig,
uploadables,
) {
return fetch('/graphql', {
method: 'POST',
headers: {
// Add authentication and other headers here
'content-type': 'application/json'
},
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
}).then(response => {
return response.json();
});
}
// Create a network layer from the fetch function
const network = Network.create(fetchQuery);
If this is the case, write your fetchQuery function to fetch data using a multipart form. See this example: fetch post with multipart form data

Parse 'form-data' from incoming webhook using Google Apps Script

I have a (mulitlevel?) webhook sending into a Google Script (Access:" Anyone"/"Anyone even anonymous") and trying to pull the info out of it with a doPost(). I've used ngrok to capture a sample but can't make it work. I'm trying to extract event but getting errors like
'TypeError Cannot read property 'contents' of undefined'.
I'm using this as one possible starting point to parse it.
function doPost(e) {
try{
var data = JSON.parse(e.postData.contents);
var event = data.event;
} catch(err){
var mykey = '*************************************8'
var dataURL = 'https://maker.ifttt.com/trigger/Data_Check_WHook/with/key/'+mykey+'?value1=Error:&value2=error: '+event
UrlFetchApp.fetch(dataURL);
}
Below a sample of the webhook. Do I need to access 'payload' before reading 'event'?
Content-Disposition: form-data; name="payload"
Content-Type: application/json
{"event":"media.pause","user":true,"owner":true,"Account":********************************Hours_2973","summary":"","viewOffset":1391000,"lastViewedAt":1592403577,"year":2019,"thumb":"/library/metadata/5448/thumb/1572876947","art":"/library/metadata/5448/art/1572876947","originallyAvailableAt":"2019-08-15","addedAt":1560188503,"updatedAt":1572876947,"createdAtAccuracy":"epoch","createdAtTZOffset":"3600"}}
--------------------------55b945c1eb00cab0--

Add flag to UnityWebRequest

I am trying to make a POST request to webpage that expects the --data field to be filled with some data to be processed. I'm pretty much trying to recreate this curl request, but with UnityWebRequest.
curl -X POST http://localhost:8000/clic/say?text=Make+the+gene+set --data '{"geneSetMembers":["UST"],"geneSetName":"selection0"}'
The UnityWebRequest documentation mentions that GET requests don't set any flags other than the url, but it's not clear if no other custom options exist for posts. Is there some way to format a WWWform or something that will hold the data such that the server will recognize it?
var form = new WWWForm();
// some way to plug in the jsonified data to the form
webRequest = UnityWebRequest.Post(url + route + to_say, form);
webRequest.downloadHandler = new DownloadHandlerBuffer();
webRequest.SetRequestHeader("Content-Type", "application/json");
webRequest.SendWebRequest();
// etc etc
I've tried just giving the form a field named "data" a la
form.AddField("data", "{ \"geneSetMembers\":[\"UST\"],\"geneSetName\":\"selection0\"}");
but the server does not like it, saying it "got error Invalid JSON literal name: data" So clearly that's the wrong syntax for it
EDIT: put lines in the same order they were in original code. Sorry, I have commented lines between them
Maybe your server doesn't like to receive the data as a field called data.
This ofcourse depends totally on the PHP code we don't see since you didn't share that part. b
But at least I can tell you that --data or also simply -d in curl refer to the entire data section and is not a field called data.
You could try to instead use a MultiPartFormDataSection passing just the data itself without a specific field name
var data = "{\"geneSetMembers\":[\"UST\"],\"geneSetName\":\"selection0\"}";
var form = new List<IMultiFormPart>{ new MultiPartFormDataSection(data) };
webRequest = UnityWebRequest.Post(url + route + to_say, form);
yield return webRequest.SendWebRequest();
which is now sent as content-type multipart/form-data though ...
Another alternative if your server really needs to receive a content-type application/json might be to "manually" compose the request e.g. like
var data = "{\"geneSetMembers\":[\"UST\"],\"geneSetName\":\"selection0\"}";
var request = new UnityWebRequest(url + route + to_say, "POST");
var bodyRaw = Encoding.UTF8.GetBytes(data);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
Though of you look close now this seems actually not to be the case since if you read the man curl
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded
which is actually exactly the default content type used by the simple string version of UnityWebRequest.Post.
So thinking about it it should actually be as simple as using the pure string version of UnityWebRequest.Post:
var data = "{\"geneSetMembers\":[\"UST\"],\"geneSetName\":\"selection0\"}";
var request = UnityWebRequest.Post(url + route + to_say, data);
yield return request.SendWebRequest();