質問編集履歴

2

記事の確認

2023/06/28 01:55

投稿

kouji_39
kouji_39

スコア164

test CHANGED
File without changes
test CHANGED
@@ -40,8 +40,7 @@
40
40
  919 @property
41
41
 
42
42
  JSONDecodeError: [Errno Expecting value] <!DOCTYPE html>
43
- ```#コード実行後の出力↓
43
+ ```
44
-
45
44
 
46
45
  ### 該当のソースコード
47
46
 

1

この質問は、「この回答はGPT-4による自動生成回答」を踏まえ、再度、質問内容を変更

2023/06/28 01:53

投稿

kouji_39
kouji_39

スコア164

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 実現したいこと
2
2
 
3
3
  pythonによるAPI利用(メソッドPOST)で、bodyを取得したい
4
-
4
+ この質問は、「この回答はGPT-4による自動生成回答」を踏まえ、再度、質問内容を変更したものです。
5
5
  ### 前提
6
6
 
7
7
  ・データベースで、APIキーを購入し、欲しい条件の施設のID番号を取得するタスクがあります。
@@ -16,21 +16,42 @@
16
16
 
17
17
 
18
18
  ### 発生している問題・エラーメッセージ
19
+ 修正コードで実行したら、JSONDecodeError: [Errno Expecting value] となりました。
20
+ ```ここに言語を入力
21
+ 200
22
+ ---------------------------------------------------------------------------
23
+ JSONDecodeError Traceback (most recent call last)
24
+ /usr/local/lib/python3.10/dist-packages/requests/models.py in json(self, **kwargs)
25
+ 909 try:
26
+ --> 910 return complexjson.loads(self.text, **kwargs)
27
+ 911 except JSONDecodeError as e:
28
+
29
+ 4 frames
30
+ JSONDecodeError: Expecting value: line 1 column 1 (char 0)
31
+
32
+ During handling of the above exception, another exception occurred:
33
+
34
+ JSONDecodeError Traceback (most recent call last)
35
+ /usr/local/lib/python3.10/dist-packages/requests/models.py in json(self, **kwargs)
36
+ 915 raise RequestsJSONDecodeError(e.message)
37
+ 916 else:
38
+ --> 917 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
39
+ 918
19
- エラーメッセージは無い。
40
+ 919 @property
41
+
42
+ JSONDecodeError: [Errno Expecting value] <!DOCTYPE html>
43
+ ```#コード実行後の出力↓
44
+
20
45
 
21
46
  ### 該当のソースコード
22
47
 
23
48
  ```python3
24
49
  import requests
25
50
  import json
26
- import csv
27
- import pandas as pd
28
51
 
29
- #施設IDを取得する:2021年度のレポートより
30
52
  url = "https://xxxxxxxxxxx/api/xx/xxxx/xxxx/report/2021/"
31
- headers = {'api-key': '〇〇〇〇〇〇'}#〇〇〇〇〇〇は、購入キー
53
+ headers = {'api-key': '〇〇〇〇〇〇', 'Content-Type': 'application/json'}
32
54
 
33
- #リクエストパラメータ指定
34
55
  data = {
35
56
  "querys":[
36
57
  {
@@ -48,17 +69,16 @@
48
69
  "offset":0
49
70
  }
50
71
 
51
- res = requests.post(url, data=data)
72
+ res = requests.post(url, headers=headers, data=json.dumps(data))
52
- print(res.status_code)#200を出力
73
+ print(res.status_code)
53
74
 
54
- with open('./output2.txt', 'w') as f:
55
- f.write(res.text)#textが出力されるがbodyにあたる内容が含まれない
75
+ print(res.json()) # レスポンスボディを JSON 形式で出力
56
-
57
76
  ```
58
77
 
59
78
  ### 試したこと
60
79
 
61
80
  post、requests、python等をキーワード検索したが、有効な情報が無かった。
81
+ この質問は、「この回答はGPT-4による自動生成回答」を踏まえ、再度、質問内容を変更したものです。
62
82
 
63
83
  ### 補足情報(FW/ツールのバージョンなど)
64
84