purescript-contrib / purescript-affjax

An asynchronous AJAX library built using Aff.
Apache License 2.0
121 stars 78 forks source link

Query Issues with React Native #163

Closed kodeFant closed 2 years ago

kodeFant commented 2 years ago

Hi, I am currently using affjax in a React Native environment. More specifically with React.Basic.Native. I also installed xhr2 to be sure. I know React Native primarily supports the fetch api.

Describe the bug Fetching from a simple enpoint work well, but all query parameters are ignored.

I first tried it like this with query string in URL

getProductComments :: Product -> Aff (RemoteData String (Array Comment))
getProductComments product = do
  res <-
    AX.request
      ( AX.defaultRequest
          { url = apiUrl <> "/ProductComments?productId=" <> product.id
          , method = Left GET
          , responseFormat = ResponseFormat.json
          , headers = unauthenticatedHeaders
          }
      )
  pure (responseToRemoteData res decodeComments)

It seems to strip away the query parameters and only the query without any params are run.

Same thing actually happens when using a response body only.

getProductComments :: Product -> Aff (RemoteData String (Array Comment))
getProductComments product = do
  res <-
    AX.request
      ( AX.defaultRequest
          { url = apiUrl <> "/ProductComments"
          , method = Left GET
          , responseFormat = ResponseFormat.json
          , content = Just (RequestBody.json $ encodeJson { productId: product.id })
          , headers = unauthenticatedHeaders
          }
      )
  pure (responseToRemoteData res decodeComments)

The API supports both query params and body params and I have tested this on postman and can verify it works.

Expected behavior Query strings should be registered and not stripped away.

Additional context

garyb commented 2 years ago

I don't know anything about React Native, and very little about fetch too, so hopefully someone else can chime in here who has ideas. :smile:

One option might be to avoid this library and use purescript-web-fetch directly or something like that.

kodeFant commented 2 years ago

Thanks for reply :) Closing the issue since I am solving the problems elsewhere.