PyCharm 2018.3 Help

Exploring the HTTP request in Editor syntax

To compose an HTTP request in the PyCharm code editor, use the following general syntax:

Method Request-URI HTTP-Version Header-field: Header-value Request-Body

The HTTP request in Editor format introduces additional capabilities, as demonstrated by the following examples. For details on working with HTTP requests, see HTTP Client in PyCharm Code Editor.

Use comments in HTTP requests

  • Within a request, start any line with with // or # to make it a comment line.

    // A basic request GET http://example.com/a/

Use short form for GET requests

  • For GET requests, you can omit the request method and only specify the URI.

    // A basic request http://example.com/a/

Compose several requests in a single file

  1. Mark the end of a request by typing the ### separator below it.

    // A basic request http://example.com/a/ ###
  2. Compose another request below the separator.

    // A basic request http://example.com/a/ ### // A second request using the GET method http://example.com:8080/api/html/get?id=123&value=content

Break long requests into several lines

  • Indent all query string lines but the first one.

    // Using line breaks with indent GET http://example.com:8080 /api /html /get ?id=123 &value=content

Access a web service with authentication

  • Depending on the web service you are accessing, provide the basic or digest Authorization header.

    // Basic authentication GET http://example.com Authorization: Basic username password ### // Digest authentication GET http://example.com Authorization: Digest username password

Enable or disable following redirects

Depending on the web service you are accessing, you may want HTTP requests to either follow redirects or not. When a redirect is followed, the redirected page response is returned; otherwise, the actual redirect response header (such as 301 or 302) is returned.

  • Before the request, add a comment line with the @no-redirect tag.

    // @no-redirect example.com/status/301

Provide the request message body

  • Inside the request, prepend the request body with a blank line and do one of the following:

    • Type the request body in place:

      // The request body is provided in place POST http://example.com:8080/api/html/post HTTP/1.1 Content-Type: application/json Cookie: key=first-value { "key" : "value", "list": [1, 2, 3] }

      If you set the Content-Type header field value to one of the languages supported by PyCharm, then the corresponding language fragment will be auto-injected into the HTTP request message body. If Content-Type is not specified, you can inject a language fragment manually. For more information, see Using Language Injections.

    • To read the request body from a file, type the < symbol followed by the path to the file.

      // The request body is read from a file POST http://example.com:8080/api/html/post Content-Type: application/json < ./input.json

Use multipart/form-data content type

  • Set the request's Content-Type to multipart/form-data. To send a file as part of the multipart/form-data message, include the filename parameter in the Content-Disposition header.

    POST http://example.com/api/upload HTTP/1.1 Content-Type: multipart/form-data; boundary=boundary --boundary Content-Disposition: form-data; name="first"; filename="input.txt" // The 'input.txt' file will be uploaded < ./input.txt --boundary Content-Disposition: form-data; name="second"; filename="input-second.txt" // A temporary 'input-second.txt' file with the 'Text' content will be created and uploaded Text --boundary Content-Disposition: form-data; name="third"; // The 'input.txt' file contents will be sent as plain text. < ./input.txt --boundary--
Last modified: 10 December 2018