2
0

More than 1 year has passed since last update.

【#41 エンジニア転職学習】HTTP POSTメソッド作成時の動作確認エラー

Posted at

はじめに

富山県に住んでいるChikaといいます。
毎日投稿を目標に、バックエンドエンジニア転職に向けた学習内容をアウトプットします。

7月いっぱいまでGoのフレームワークであるGinについて、
学習をしていきます。

バックエンドエンジニアになるまでの学習内容は以前投稿した以下の記事を基にしています。

本日の学習内容

GinのTutorialとして簡単なRESTfulAPI作成の学習をしました。

  • POSTメソッド作成 動作確認時のエラー ←Topics!!
  • 特定のアイテムを返すGETメソッド作成

POSTメソッド作成時の動作確認エラー

本日もGinのAPI作成チュートリアルを進めていたのですが、
その中でターミナルでのcurlコマンドを使用した
POSTリクエストの動作確認時にエラーが発生して、解決に手間取りましたのでまとめます。

解決策

header情報内の"の前に\を入れてエスケープさせる

原因

Windows環境でcurlコマンドを使用したとき限定の不具合のようです
Linux等では問題なく実行できるようなのでWindowsのみ気を付ける必要あり

エラーが発生したコマンド

curl -X POST -H "Content-Type:aplication/json"  
   -d "{"id": "4","title": "The Modern Sound of Betty Carter", 
   "artist": "Betty Carter","price": 49.99}" "http://localhost:8080/albums"

エラーメッセージ

curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: Modern
curl: (6) Could not resolve host: Sound
curl: (6) Could not resolve host: of
curl: (6) Could not resolve host: Betty
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 20:
Carter,price: 49.99}
                   ^

修正後のコマンド

curl -X POST -H "Content-Type:aplication/json" 
   -d '{\"id\": \"4\",\"title\": \"The Modern Sound of Betty Carter\",
   \"artist\": \"Betty Carter\",\"price\": 49.99}' http://localhost:8080/albums
使用している教材はこちら↓

おわりに

最後までお読みいただきありがとうございました。
アドバイス・応援コメント等いただけますと幸いです。

2
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
2
0