# Http request with options method

**URL:** <https://discourse.elm-lang.org/t/http-request-with-options-method/1596>\
**Category:** Learn\
**Created:** [August 1, 2018, 3:53pm UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596 "2018-08-01T15:53:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdxiong](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/tdxiong/32/986_2.png) [@tdxiong](https://discourse.elm-lang.org/u/tdxiong)\
**Post date:** [August 1, 2018, 3:53pm UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/1 "2018-08-01T15:53:48Z")

</div>

I want to fetch a url in the form of “[https://domain.com?a=a&b=b](https://domain.com?a=a&b=b)”, below code can work without problem:

fetchIndexCommand : Cmd Msg  
fetchIndexCommand =  
Http.get “[https://domain.com?a=a&b=b](https://domain.com?a=a&b=b)” indexDecoder  
|\> RemoteData.sendRequest  
|\> Cmd.map IndexReceived

I am trying to make it another way, make the parameters seperated, as below:

fetchIndexCommand : Cmd Msg  
fetchIndexCommand =  
indexRequest “[https://domain.com](https://domain.com)” indexDecoder  
|\> RemoteData.sendRequest  
|\> Cmd.map IndexReceived

indexRequest : String -\> Decoder a -\> Http.Request a  
indexRequest url decoder =  
Http.request  
{ method = “GET”  
, headers = []  
, url = url  
, body = Http.jsonBody indexEncoder  
, expect = Http.expectJson decoder  
, timeout = Nothing  
, withCredentials = False  
}

indexEncoder : Encode.Value  
indexEncoder =  
Encode.object  
[ ( “a”, Encode.string “a” )  
, ( “b”, Encode.string “b” )  
]

This will not work, it will return NetworkError of RemoteData, my browser console will show OPTIONS 405 error, as well as on the server end log: REST Method Not Supported (405): Method options not supported at…

The Http.get is also Http.request, why the behaviour is so different? Did I make any mistake?

---

<div class="post-metadata">

**Author:** ![sam](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sam/32/1843_2.png) [@sam](https://discourse.elm-lang.org/u/sam)\
**Post date:** [August 1, 2018, 4:18pm UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/2 "2018-08-01T16:18:37Z")

</div>

Is “[https://domain.com?a=a&b=b”](https://domain.com?a=a&b=b%E2%80%9D) the actual url you are fetching? Also what domain is your elm app running on?

This sounds very much like a [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) issue. If the second request is not a “simple request” (see the section on the linked page for details on what that is) then the browser will automatically send an HTTP `OPTIONS` pre-flight request first to check whether the CORS settings for that site will allow your actual request. From your error message (“Method Not Supported (405): Method options not supported…”) it sounds ike the http server doesn’t support `OPTIONS` http requests and therefore the CORS pre-flight request fails.

---

<div class="post-metadata">

**Author:** ![tdxiong](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/tdxiong/32/986_2.png) [@tdxiong](https://discourse.elm-lang.org/u/tdxiong)\
**Post date:** [August 1, 2018, 4:29pm UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/3 "2018-08-01T16:29:09Z")

</div>

my elm app is on my local machine, fetching from remote. So my server has to accept OPTIONS method.

Thank you. I will try to configure my server

---

<div class="post-metadata">

**Author:** ![wondible](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wondible](https://discourse.elm-lang.org/u/wondible)\
**Post date:** [August 2, 2018, 11:32am UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/4 "2018-08-02T11:32:01Z")

</div>

indexRequest is a GET, which does not send a body. You would need to POST (maybe PUT, depending on the use case) in order to send a body. The server will of course need to support these methods and handle parsing the body.

---

<div class="post-metadata">

**Author:** ![tdxiong](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/tdxiong/32/986_2.png) [@tdxiong](https://discourse.elm-lang.org/u/tdxiong)\
**Post date:** [August 6, 2018, 2:13am UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/5 "2018-08-06T02:13:17Z")

</div>

Indeed. So to send a GET request, the parameters have to been added to url as “[domain.com?a=a&b=b](http://domain.com?a=a&b=b)”, No way to encode seperately?

---

<div class="post-metadata">

**Author:** ![jessta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jessta/32/660_2.png) [@jessta](https://discourse.elm-lang.org/u/jessta)\
**Post date:** [August 6, 2018, 7:17am UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/6 "2018-08-06T07:17:43Z")

</div>

You can use [http://package.elm-lang.org/packages/sporto/qs/1.0.0/QS#serialize](http://package.elm-lang.org/packages/sporto/qs/1.0.0/QS#serialize) to encode the query string for a url. If you have a lot of query parameters or a complex use case this may be helpful.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [August 16, 2018, 7:17am UTC](https://discourse.elm-lang.org/t/http-request-with-options-method/1596/7 "2018-08-16T07:17:44Z")

</div>

Closed on August 16, 2018, 7:17am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
