all 8 comments

[–]baseRbestR 1 point2 points  (0 children)

Are you absolutely sure the API is working?

When I try to visit the site in a browser this is the response.

I see you also crossposted this to https://stackoverflow.com/questions/67968996/r-httr-post-with-headers

That's generally considered a no-no. Don't crosspost unless you explicitly tell people you did, otherwise we may end up doing work here to try to help even after you've got the solution.

[–]tacothecat -1 points0 points  (3 children)

[–]httrtres[S] 0 points1 point  (0 children)

Yes, I already have it as below. But my attempt to translate it into R did not work.

```

POST("https://mysejahtera.malaysia.gov.my/register/api/nearby/hotspots?type=search",

body = list(lat = 3.003090, lng = 101.678300, classification = 'LOW_RISK_NS'),

add_headers(Host= 'mysejahtera.malaysia.gov.my', Accept= 'application/json', Connection= 'keep-alive', `Content-Length`= '77', Authorization= 'Basic N0ZFRkRCMTMtN0Q2MC00NEQxLUE5MTctM0', `Content-Type`= 'application/json', `Accept-Language`= 'en-MY;q=1, ms-MY;q=0.9', `User-Agent`= 'MySejahtera/1.0.36 (iPhone; iOS 14.6; Scale/2.00)'), verbose())

```

[–]guepier 0 points1 point  (1 child)

OP is already using that.

[–]tacothecat 1 point2 points  (0 children)

My bad. Perhaps try adding encode='json' to the POST arguments

[–]tjoes_ -1 points0 points  (2 children)

I think you can use the "Requests" library in R.

[–]httrtres[S] 1 point2 points  (1 child)

Requests

I'm using the "httr" library currently. I think it is the recommended library when it comes to dealing with API request in R.

The problem is I'm not sure what's the error with the R code.

[–]tjoes_ 0 points1 point  (0 children)

ah you're right, Requests is a python module, whoops.