简体   繁体   中英

`requests.post` not connecting correctly

I have a model I am trying to connect to in R. I open the model by running;

> library(plumber)
> r <- plumb("deploy_ml_credit_model.R")
Warning message:
In readLines(file) :
  incomplete final line found on 'deploy_ml_credit_model.R'
> r$run(swagger = FALSE)
Starting server to listen on port 3582

I then head over to Python and run the following;

Python Code:

import requests
import json
response = requests.post(
    “http://127.0.0.1:3582”
    , headers={“Content-Type”: “application/json”}
    , data=json.dumps({
        "Status.of.existing.checking.account": "A11"
    , "Duration.in.month": 24
    , "Credit.history": "A32"
    , "Savings.account.bonds": "A63"
    })
)

print response.json()

Where I obtain the following error:

  File "<ipython-input-41-507692393845>", line 4
    “http://127.0.0.1:3582”
        ^
SyntaxError: invalid character in identifier

I open the link in the browser and I obtain this message: {"error":["404 - Resource Not Found"]}

Where am I going wrong with this? I have plumber running in the background in RStudio so it should connect. Do I have some issues regarding permissions or firewall issues?

EDIT:

I am still obtaining the following message when I type http://127.0.0.1:3582/predict - the error http://127.0.0.1:3582/predict . I clean up the code and I get the following error:

Code 2:

import requests
import json
response = requests.post(
    "http://127.0.0.1:3582"
    , headers={"Content-Type": "application/json"}
    , data=json.dumps({
        "Status.of.existing.checking.account": "A11"
    , "Duration.in.month": 24
    , "Credit.history": "A32"
    , "Savings.account.bonds": "A63"
    })
)


print response.json()

Error 2:

  File "<ipython-input-49-0669c2ac9d9d>", line 15
    print response.json()
                 ^
SyntaxError: invalid syntax

EDIT 3:

I type:

print (response)

and I obtain this error:

<Response [404]>

But the R model is running in the background

EDIT:

Using:

curl -X POST "http://127.0.0.1:8000/__swagger__/predict" -d '{"Status.of.existing.checking.account": "A11", "Duration.in.month": 24, "Credit.history": "A32", "Savings.account.bonds": "A63"}' -H  "accept: application/json"

Gives this error:

curl -X POST "http://127.0.0.1:8000/__swagger__/predic
t" -d '{"Status.of.existing.checking.account": "A11", "Duration.in.month": 24, "Credit.history":
 "A32", "Savings.account.bonds": "A63"}' -H  "accept: application/json"
<h1>Bad Request</h1>curl: (6) Could not resolve host: A11,
curl: (6) Could not resolve host: Duration.in.month
curl: (6) Could not resolve host: 24,
curl: (6) Could not resolve host: Credit.history
curl: (6) Could not resolve host: A32,
curl: (6) Could not resolve host: Savings.account.bonds
curl: (3) [globbing] unmatched close brace/bracket in column 4

EDIT:

I run this line:

curl POST -H 'Content-Type:accept:application/json' -d "{/"Credit.history/":/"A32/"}" http://127.0.0.1:8000/__swagger__/predict

So I decided to remove a lot of what I want to send to the model down to just one variable and now I am getting this error:

curl POST -H 'Content-Type:accept:application/json' -d "{/"Credit.history/":/"A32/"}" http://127.0.0.1:8000/__swagger__/predict
curl: (6) Could not resolve host: POST
{"error":["500 - Internal server error"],"message":["Error: lexical error: invalid char in json text.\n                                     {/Credit.history/:/A32/}\n
           (right here) ------^\n\n"]}

You have very weird quotes in the string

“http://127.0.0.1:3582”

– they should just be straight quotes:

"http://127.0.0.1:3582"

Your "Error 2" seems to refer to using Python 3 with Python 2 syntax. print is a regular function in Py3, so you need to use parentheses around it:

import requests
import json

response = requests.post(
    "http://127.0.0.1:3582",
    headers={"Content-Type": "application/json"},
    data=json.dumps(
        {
            "Status.of.existing.checking.account": "A11",
            "Duration.in.month": 24,
            "Credit.history": "A32",
            "Savings.account.bonds": "A63",
        }
    ),
)


print(response.json())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM