Hey! I'm working on a small toy project which I needed a Http POST for as one of the steps.
I've set everything up following the docs and ended up with some encoded json and a request that looks more or less like this:
{ Http.defaultRequest &
url: "https://invoice-generator.com",
method: Post,
headers: [Http.header "Content-Type" "application/json"],
body,
}
but the API I was calling was giving me strange results, like I never sent any JSON. I debugged the problem a little bit, and added some logging Rust side, and noticed that my request contains headers that go like this:
headers: {"content-type": "", "content-type": "application/json"}
which lead me to find out that mimeType from Http.defaultRequest
is always appended to the headers as content-type even if it's an empty string (default value there), which then seems to break the API call.
An obvious solution for that was to get rid of headers
in my Request and just set the request like this:
{ Http.defaultRequest &
url: "https://invoice-generator.com",
method: Post,
body,
mimeType: "application/json"
}
and this solves the problem, and the API call is being consumed correctly.
Now, I think it'd be nice to have some checks in the Rust code that'd prevent this form happening, eg. if mimeType is "" then do not append it to headers at all, or if both headers and mimeType set the same content type, use only one etc. I'd be happy to experiment with that if you folks think it makes sense as an improvement. I have to admit I spent quite some time figuring out what's going on there so I assume other beginners could run into the same problem too :smile:
Sounds good. If you do some investigation and come up with something, I'll be happy to look at it.
Awesome! Thanks for your quick response. I'll mingle with it asap :).
As promised, I gave it a shot, and here's a PR with my proposed change https://github.com/roc-lang/basic-cli/pull/179 @Luke Boswell please let me know if it makes sense and if I can somehow improve! Thanks a lot :smile:
Piotr Brzeziński has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC