Python urllib to R

Python Code is works well

import os
import sys
import urllib.request
client_id = "123"
client_secret = "456"
url = "https://openapi.naver.com/v1/datalab/search";
body = "{\"startDate\":\"2021-08-01\",\"endDate\":\"2021-09-14\",\"timeUnit\":\"date\",\"keywordGroups\":[{\"groupName\":\"Google\",\"keywords\":[\"한글\",\"korean\"]}]}";

request = urllib.request.Request(url)
request.add_header("X-Naver-Client-Id",client_id)
request.add_header("X-Naver-Client-Secret",client_secret)
request.add_header("Content-Type","application/json")
response = urllib.request.urlopen(request, data=body.encode("utf-8"))
rescode = response.getcode()
if(rescode==200):
    response_body = response.read()
    print(response_body.decode('utf-8'))
else:
    print("Error Code:" + rescode)

Buy my R code is not work

require(httr)

headers = c(
  'X-Naver-Client-Id' = '123',
  'client_secret' = '456'
)

data = "{\"startDate\":\"2021-08-01\",\"endDate\":\"2021-09-14\",\"timeUnit\":\"date\",\"keywordGroups\":[{\"groupName\":\"google\",\"keywords\":[\"한글\",\"korean\"]}]}"

res <- httr::POST(url = 'https://openapi.naver.com/v1/datalab/search', httr::add_headers(.headers=headers), body = data)

What is problem?
I think the header is not delivered.
I searched a lot, but I don't know. I need help.

Seeing the error message from httr, particularly the HTTP response status codes, would be helpful in diagnosing things (if you share these, be careful to scrub any private info from the message).

In the Python case you encode the data.
"For reasons of symmetry" I would expect this also in the R case?

I solved this problem. The method below.
Thanks!!!

r <- POST("url~~~", body=bodys~~~~,
httr::add_headers(.headers = c(headers~~~),
httr::content_type('application/json'))

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.