post example

$queryParams = [
            'method' => 'call',
            'arg0' => 'GET',
            'arg1' => 'product',
            'arg2' => json_encode($requestPayload),
        ];

        $queryParams = http_build_query($queryParams, null, '&');

        $url = $this->baseUrl . '?' . $queryParams;

        $response = $this->client->post($url);

        $result = simplexml_load_string($response->getBody());
        $result = $result->call;

        if ($result->status == 'failed') {
            throw new RuntimeException($result->response->message);
        }

        $result = $result->response->_search_result;
        $result = json_decode($result);
        $result = json_decode($result->_products, true);

        
$this->submitForm($edit, 'Save configuration');
    $config = $this->config('statistics.settings');
    $this->assertNotEmpty($config->get('count_content_views'), 'Count content view log is enabled.');

    // Hit the node.     $this->drupalGet('node/' . $this->testNode->id());
    // Manually calling statistics.php, simulating ajax behavior.     $nid = $this->testNode->id();
    $post = ['nid' => $nid];
    global $base_url;
    $stats_path = $base_url . '/' . $this->getModulePath('statistics') . '/statistics.php';
    $this->client->post($stats_path['form_params' => $post]);

    // Hit the node again (the counter is incremented after the hit, so     // "1 view" will actually be shown when the node is hit the second time).     $this->drupalGet('node/' . $this->testNode->id());
    $this->client->post($stats_path['form_params' => $post]);
    $this->assertSession()->pageTextContains('1 view');

    $this->drupalGet('node/' . $this->testNode->id());
    $this->client->post($stats_path['form_params' => $post]);
    $this->assertSession()->pageTextContains('2 views');

    
public function testCorrectSignRequest(): void
    {
        $optionsRequest
            = [AuthMiddleware::APP_REQUEST_TYPE => [
                AuthMiddleware::APP_SECRET => 'secret',
            ],
                'body' => 'test', ];

        $this->appendNewResponse(new Response(200));

        $client = $this->getContainer()->get('shopware.app_system.guzzle');
        $client->post(new Uri('https://example.local')$optionsRequest);

        /** @var Request $request */
        $request = $this->getLastRequest();

        static::assertArrayHasKey(RequestSigner::SHOPWARE_SHOP_SIGNATURE, $request->getHeaders());
    }

    public function testMissingRequiredResponseHeader(): void
    {
        $this->appendNewResponse(new Response(200));

        
'pass' => $account->passRaw,
      'op' => 'Log in',
    ];
    $url = $this->buildUrl($this->httpUrl('user/login'));
    // When posting directly to the HTTP or HTTPS mock front controller, the     // location header on the returned response is an absolute URL. That URL     // needs to be converted into a request to the respective mock front     // controller in order to retrieve the target page. Because the URL in the     // location header needs to be modified, it is necessary to disable the     // automatic redirects normally performed by the Guzzle CurlHandler.     /** @var \Psr\Http\Message\ResponseInterface $response */
    $response = $this->getHttpClient()->post($url[
      'form_params' => $post,
      'http_errors' => FALSE,
      'cookies' => $guzzle_cookie_jar,
      'allow_redirects' => FALSE,
    ]);

    // When logging in via the HTTP mock, the child site will issue a session     // cookie without the secure attribute set. While this cookie will be stored     // in the Guzzle CookieJar, it will not be used on subsequent requests.     // Update the BrowserKit CookieJar so that subsequent requests have the     // correct cookie.
 {
    }

    /** * @param class-string<AbstractResponse> $responseClass */
    public function request(string $url, SourcedPayloadInterface $payload, AppEntity $app, string $responseClass, Context $context): ?Struct
    {
        $optionRequest = $this->getRequestOptions($payload$app$context);

        try {
            $response = $this->client->post($url$optionRequest);

            $content = $response->getBody()->getContents();

            $transactionId = null;
            if ($payload instanceof PaymentPayloadInterface) {
                $transactionId = $payload->getOrderTransaction()->getId();
            }

            return $responseClass::create($transactionIdjson_decode($content, true, 512, \JSON_THROW_ON_ERROR));
        } catch (GuzzleException) {
            return null;
        }
$csrf_token = $this->drupalGet($csrf_token_path);
    $url = Url::fromRoute($route_name)
      ->setAbsolute(TRUE)
      ->toString();
    $post_options = [
      'headers' => ['Accept' => 'text/plain'],
      'http_errors' => FALSE,
    ];

    // Test that access is allowed for anonymous user with no token in header.     $result = $client->post($url$post_options);
    $this->assertEquals(200, $result->getStatusCode());

    // Add cookies to POST options so that all other requests are for the     // authenticated user.     $post_options['cookies'] = $this->getSessionCookies();

    // Test that access is denied with no token in header.     $result = $client->post($url$post_options);
    $this->assertEquals(403, $result->getStatusCode());

    // Test that access is allowed with correct token in header.

        if (in_array('new', $methods, true)) {
            $this->get($name . '/new', $newName . '::new', $options);
        }
        if (in_array('edit', $methods, true)) {
            $this->get($name . '/' . $id . '/edit', $newName . '::edit/$1', $options);
        }
        if (in_array('show', $methods, true)) {
            $this->get($name . '/' . $id$newName . '::show/$1', $options);
        }
        if (in_array('create', $methods, true)) {
            $this->post($name$newName . '::create', $options);
        }
        if (in_array('update', $methods, true)) {
            $this->put($name . '/' . $id$newName . '::update/$1', $options);
            $this->patch($name . '/' . $id$newName . '::update/$1', $options);
        }
        if (in_array('delete', $methods, true)) {
            $this->delete($name . '/' . $id$newName . '::delete/$1', $options);
        }

        // Web Safe? delete needs checking before update because of method name         if (isset($options['websafe'])) {
            
$account = $this->drupalCreateUser([]);
    $post = [
      'form_id' => 'user_login_form',
      'form_build_id' => $form_build_id,
      'name' => $account->getAccountName(),
      'pass' => $account->passRaw,
      'op' => 'Log in',
    ];
    $url = $this->buildUrl(Url::fromRoute('user.login'));

    /** @var \Psr\Http\Message\ResponseInterface $response */
    $response = $this->getHttpClient()->post($url[
      'form_params' => $post,
      'http_errors' => FALSE,
      'cookies' => FALSE,
      'allow_redirects' => FALSE,
    ]);

    // Follow the location header.     $this->drupalGet($response->getHeader('location')[0]);
    $this->assertSession()->statusCodeEquals(403);
    $this->assertSession()->pageTextContains('To log in to this site, your browser must accept cookies from the domain');
  }

  

    public function request(
        string $url,
        SourcedPayloadInterface $payload,
        AppEntity $app,
        string $responseClass,
        Context $context
    ): ?Struct {
        $optionRequest = $this->getRequestOptions($payload$app$context);

        try {
            $response = $this->client->post($url$optionRequest);

            $content = $response->getBody()->getContents();

            $transactionId = null;
            if ($payload instanceof PaymentPayloadInterface) {
                $transactionId = $payload->getOrderTransaction()->getId();
            }

            return $responseClass::create($transactionId, \json_decode($content, true, 512, \JSON_THROW_ON_ERROR));
        } catch (GuzzleException) {
            return null;
        }
->setRouteParameter('_format', $format)
      ->setAbsolute();

    $request_body = [];
    if (isset($name)) {
      $request_body['name'] = $name;
    }
    if (isset($pass)) {
      $request_body['pass'] = $pass;
    }

    $result = \Drupal::httpClient()->post($user_login_url->toString()[
      'body' => $this->serializer->encode($request_body$format),
      'headers' => [
        'Accept' => "application/$format",
      ],
      'http_errors' => FALSE,
      'cookies' => $this->cookies,
    ]);
    return $result;
  }

  /** * Tests user session life cycle. */


        return new JsonResponse($actionResponse);
    }

    /** * @param array<mixed> $payload */
    private function executeHttpRequest(AppAction $action, Context $context, array $payload, string $appSecret): string
    {
        try {
            $response = $this->guzzleClient->post(
                $action->getTargetUrl(),
                [
                    AuthMiddleware::APP_REQUEST_CONTEXT => $context,
                    AuthMiddleware::APP_REQUEST_TYPE => [
                        AuthMiddleware::APP_SECRET => $appSecret,
                        AuthMiddleware::VALIDATED_RESPONSE => true,
                    ],
                    'json' => $payload,
                ]
            );

            

    public function doTrackEvent($eventName$additionalInformation = [])
    {
        $payload = [
            'additionalData' => $additionalInformation,
            'instanceId' => $this->uniqueId,
            'event' => $eventName,
        ];

        try {
            $response = $this->client->post($this->apiEndPoint . '/tracking/events', json_encode($payload));
        } catch (Exception $ex) {
            return false;
        }

        return json_decode($response->getBody(), true) ?: false;
    }
}
static::assertIsString($schema);

        $this->assertValidSchema($schema);
    }

    private function assertValidSchema(string $schema): void
    {
        $client = new Client();
        $validatorURL = EnvironmentHelper::getVariable('SWAGGER_VALIDATOR_URL', 'https://validator.swagger.io/validator/debug');
        static::assertIsString($validatorURL);

        $response = $client->post($validatorURL[
            'json' => json_decode($schema, true, 512, \JSON_THROW_ON_ERROR),
            'headers' => [
                'Accept' => 'application/json',
            ],
        ]);
        $content = json_decode((string) $response->getBody(), true, flags: \JSON_THROW_ON_ERROR);

        // The CI validator returns an empty response if the schema is valid         // The public Web validator returns an object with an empty schemaValidationMessages array         $messages = $content['schemaValidationMessages'] ?? [];

        
$response->getHeaders(),
            (string) $response->getBody()
        );
    }

    /** * {@inheritdoc} */
    public function post($url = null, array $headers = []$content = null): Response
    {
        try {
            $response = $this->guzzleClient->post($url$this->formatOptions($headers$content));
        } catch (Exception $e) {
            $body = '';

            if (($e instanceof GuzzleClientException) && $e->hasResponse()) {
                $body = (string) $e->getResponse()->getBody();
            }

            throw new RequestException($e->getMessage()$e->getCode()$e$body);
        }

        return new Response(
            


    public function request(
        string $url,
        TaxProviderPayload $payload,
        AppEntity $app,
        Context $context
    ): ?TaxProviderResult {
        $optionRequest = $this->getRequestOptions($payload$app$context);

        try {
            $response = $this->client->post($url$optionRequest);
            $content = $response->getBody()->getContents();

            return TaxProviderResponse::create(\json_decode($content, true, 512, \JSON_THROW_ON_ERROR));
        } catch (GuzzleException) {
            return null;
        }
    }

    /** * @return array<string, mixed> */
    
Home | Imprint | This part of the site doesn't use cookies.