sergio

sergio

How to convert this CURL request with a -F param to a Req request?

This CURL request works fine:

curl -X POST \
-H "Ocp-Apim-Subscription-Key: 123123123123123" \
-F "knowledgeRequest={\"imageInfo\":{\"url\":\"https://i.ebayimg.com/images/g/Mn8AAOSwoLtgZ2au/s-l1600.webp\"}}" \
"https://api.bing.microsoft.com/v7.0/images/visualsearch?mkt=en-us"

And here’s what I’ve tried to run this in Elixir using Req:

url = "https://api.bing.microsoft.com/v7.0/images/visualsearch?mkt=en-us"
api_key = System.get_env("MICROSOFT_OCP_APIM_SUBSCRIPTION_KEY")

headers = [
  {"Ocp-Apim-Subscription-Key", api_key}
]

# Doesn't work.
knowledge_request = "{\"imageInfo\":{\"url\":\"#{image_url}\"}}"

# Doesn't work.
knowledge_request =
  %{
    imageInfo: %{
      url: image_url
    }
  }
  |> Jason.encode!()

case Req.post(url, headers: headers, form: [knowledgeRequest: knowledge_request]) do
  {:ok, response} ->
    response

  {:error, reason} ->
    {:error, reason}
end

Error comes back as:

** (ArgumentError) errors were found at the given arguments:

  • 1st argument: not an iodata term

    :erlang.iolist_to_binary(%{“_type” => “ErrorResponse”, “errors” => [%{“code” => “InvalidRequest”, “message” => “Parameter has invalid value.”, “moreDetails” => “Parameter has invalid value.”, “parameter” => “knowledgeRequest”, “subCode” => “ParameterInvalidValue”}], “instrumentation” => %{“_type” => “ResponseInstrumentation”}})
    (jason 1.4.4) lib/jason.ex:69: Jason.decode/2
    (shopnik 0.1.0) lib/shopnik/bing_image_search.ex:22: Shopnik.BingImageSearch.parse_response/1
    iex:12: (file)

Appreciate any tips!

Most Liked

dimitarvp

dimitarvp

Check this library out: CurlReq — CurlReq v0.98.5

The author says it can convert in both directions.

wojtekmach

wojtekmach

Hex Core Team

form sets application/x-www-form-urlencoded, I believe you want multipart/form-data which is available in recent Req releases using :form_multipart. Something like this should work:

Req.post(url, headers: headers, form_multipart: [knowledgeRequest: knowledge_request])

Please let me know and/or open an issue otherwise!

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement