0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

curl でクエリパラメータをURLエンコーディング

Posted at

ISO8601形式の日付をクエリパラメータで受け取るAPIがあるとして, curl で次のように呼び出そうとすると, 失敗します.

curlによるリクエスト
curl -v -G \
    -d"until=2023-12-31T00:00:00+09:00" \
     "http://localhost:8000/samples"
エラー
{
  "detail": [
    {
      "type": "datetime_parsing",
      "loc": [
        "query",
        "until"
      ],
      "msg": "Input should be a valid datetime, invalid timezone sign",
      "input": "2023-12-31T00:00:00 09:00",
      "ctx": {
        "error": "invalid timezone sign"
      },
      "url": "https://errors.pydantic.dev/2.5/v/datetime_parsing"
    }
  ]
}

URLエンコーディングによって, + がスペース文字に置き換えられているためです.

その場合は, -d オプションの代わりに --data-urlencode オプションを使うといいようです.

curlによるリクエスト 2
curl -v -G \
    --data-urlencode "until=2023-12-31T00:00:00+09:00" \
     "http://localhost:8000/samples"

このくらいのこと, curl がよしなにやってくれているものだと思ってましたが, そうではなかったようです.

なお, httpie ならURLエンコーディングを意識せずにリクエストできます.

httpie によるリクエスト
http get "http://localhost:8000/samples" \
    until==2023-12-31T00:00:00+09:00
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?