I need to post multipart data, however I have to set the custom header for that. I read the httpclient module, I usually use postContent when multipart-data or request when sending JSON.
I think I need to use request but I have no idea the format for body/payload I need to send, so could anyone give advice how to do that?
Thanks in advance
You need to give more info.
This example shows how to handle posting data: https://nim-lang.org/docs/httpclient.html#using-http-post
Without knowing what data you want to send I can't help much more.
My bad, I thought from multipart data it was obvious. What I wanted to do was like this
import httpclient
var data = newMultipartData({
"data1": "value1",
"data2": "value2",
"data3": "value3"
})
var customHeaders = newHttpHeaders({
"Content-Type": "multipart/form-data"
"Custom-Key-1": "Custom-Value-1"
})
let theurl = "http://someurl.com"
var client = newHttpClient()
echo client.postContent(theurl, multipart=data)
# where to put `customHeaders` in postContent proc?
var response = client.request(theurl, httpMethod=HttpPost, body=$data,
headers=customHeaders)
# no `$` proc defined for multipart type, cannot use request proc
From this SO page , somehow I solved it.
I think what I wanted to ask was a way to stringify MultipartData type.
Thanks.
This would work if format in httpclient was exported. For now copy and paste this proc (and import random)
import httpclient
let data = newMultipartData({
"data1": "value1",
"data2": "value2",
"data3": "value3"
})
echo format(data).body