Example usage for io.netty.handler.codec.http HttpHeaderValues APPLICATION_X_WWW_FORM_URLENCODED

List of usage examples for io.netty.handler.codec.http HttpHeaderValues APPLICATION_X_WWW_FORM_URLENCODED

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderValues APPLICATION_X_WWW_FORM_URLENCODED.

Prototype

AsciiString APPLICATION_X_WWW_FORM_URLENCODED

To view the source code for io.netty.handler.codec.http HttpHeaderValues APPLICATION_X_WWW_FORM_URLENCODED.

Click Source Link

Document

"application/x-www-form-urlencoded"

Usage

From source file:io.gatling.http.client.BasicHttpTest.java

License:Apache License

@Test
void testPostWithHeadersAndFormParams() throws Throwable {
    withClient().run(client -> withServer(server).run(server -> {
        HttpHeaders h = new DefaultHttpHeaders();
        h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);

        List<Param> formParams = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            formParams.add(new Param("param_" + i, "value_" + i));
        }/*from   w w w . jav a 2 s. com*/

        Request request = new RequestBuilder(HttpMethod.POST, Uri.create(getTargetUrl())).setHeaders(h)
                .setBodyBuilder(new FormUrlEncodedRequestBodyBuilder(formParams)).build();

        server.enqueueEcho();

        client.test(request, 0, new TestListener() {

            @Override
            public void onComplete0() {
                assertEquals(200, status.code());
                for (int i = 1; i < 5; i++) {
                    assertEquals(headers.get("X-param_" + i), "value_" + i);
                }
            }
        }).get(TIMEOUT_SECONDS, SECONDS);
    }));
}

From source file:io.gatling.http.client.body.form.FormUrlEncodedRequestBodyBuilder.java

License:Apache License

@Override
public RequestBody<List<Param>> build(String contentType, Charset charset) {
    return new FormUrlEncodedRequestBody(content,
            contentType != null ? contentType : HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString(),
            charset);/*from  w ww  .j  a v  a 2 s  .c  om*/
}

From source file:io.jsync.http.impl.DefaultHttpServerRequest.java

License:Open Source License

@Override
public HttpServerRequest expectMultiPart(boolean expect) {
    if (expect) {
        String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
        if (contentType != null) {
            HttpMethod method = request.method();
            AsciiString lowerCaseContentType = new AsciiString(contentType.toLowerCase());
            boolean isURLEncoded = lowerCaseContentType
                    .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA) || isURLEncoded)
                    && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                            || method.equals(HttpMethod.PATCH))) {
                decoder = new HttpPostRequestDecoder(new DataFactory(), request);
            }//from  w ww. jav a  2  s . c  om
        }
    } else {
        decoder = null;
    }
    return this;
}

From source file:io.vertx.core.http.impl.Http2ServerRequestImpl.java

License:Open Source License

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();//from w w w.  ja  v  a  2 s  .c om
        if (expect) {
            if (postRequestDecoder == null) {
                CharSequence contentType = headers.get(HttpHeaderNames.CONTENT_TYPE);
                if (contentType != null) {
                    io.netty.handler.codec.http.HttpMethod method = io.netty.handler.codec.http.HttpMethod
                            .valueOf(headers.method().toString());
                    String lowerCaseContentType = contentType.toString().toLowerCase();
                    boolean isURLEncoded = lowerCaseContentType
                            .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString());
                    if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString())
                            || isURLEncoded)
                            && (method == io.netty.handler.codec.http.HttpMethod.POST
                                    || method == io.netty.handler.codec.http.HttpMethod.PUT
                                    || method == io.netty.handler.codec.http.HttpMethod.PATCH
                                    || method == io.netty.handler.codec.http.HttpMethod.DELETE)) {
                        HttpRequest req = new DefaultHttpRequest(
                                io.netty.handler.codec.http.HttpVersion.HTTP_1_1, method,
                                headers.path().toString());
                        req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentType);
                        postRequestDecoder = new HttpPostRequestDecoder(
                                new NettyFileUploadDataFactory(vertx, this, () -> uploadHandler), req);
                    }
                }
            }
        } else {
            postRequestDecoder = null;
        }
    }
    return this;
}

From source file:net.anyflow.menton.http.HttpClient.java

License:Apache License

protected static void setDefaultHeaders(HttpRequest httpRequest) {
    if (httpRequest.headers().contains(HttpHeaderNames.HOST) == false) {
        httpRequest.headers().set(HttpHeaderNames.HOST, httpRequest.uriObject().getHost());
    }/*  ww  w  .  j  a  v  a2 s .  co m*/
    if (httpRequest.headers().contains(HttpHeaderNames.CONNECTION) == false) {
        httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    if (httpRequest.headers().contains(HttpHeaderNames.ACCEPT_ENCODING) == false) {
        httpRequest.headers().set(HttpHeaderNames.ACCEPT_ENCODING,
                HttpHeaderValues.GZIP + ", " + HttpHeaderValues.DEFLATE);
    }
    if (httpRequest.headers().contains(HttpHeaderNames.ACCEPT_CHARSET) == false) {
        httpRequest.headers().set(HttpHeaderNames.ACCEPT_CHARSET, "utf-8");
    }
    if (httpRequest.headers().contains(HttpHeaderNames.CONTENT_TYPE) == false) {
        httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE,
                HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
    }
}

From source file:net.anyflow.menton.http.HttpRequest.java

License:Apache License

public Map<String, List<String>> parameters() {

    if (parameters != null) {
        return parameters;
    }// ww w  .  j  a  va  2s  .c  o m

    Map<String, List<String>> ret = Maps.newHashMap();

    if (HttpMethod.GET.equals(method()) || HttpMethod.DELETE.equals(method())) {
        ret.putAll((new QueryStringDecoder(uri())).parameters());
        return ret;
    } else if (headers().contains(HttpHeaderNames.CONTENT_TYPE)
            && headers().get(HttpHeaderNames.CONTENT_TYPE)
                    .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())
            && (HttpMethod.POST.equals(method()) || HttpMethod.PUT.equals(method()))) {

        ret.putAll((new QueryStringDecoder("/dummy?" + content().toString(CharsetUtil.UTF_8))).parameters());
    }

    return ret;
}

From source file:net.anyflow.menton.http.HttpRequest.java

License:Apache License

private void normalizeParameters() {
    String address = (new StringBuilder()).append(uriObject().getScheme()).append("://")
            .append(uriObject().getAuthority()).append(uriObject().getPath()).toString();

    if (HttpMethod.GET.equals(method()) || HttpMethod.DELETE.equals(method())) {
        String parameters = convertParametersToString();
        address += Strings.isNullOrEmpty(parameters) ? "" : "?" + parameters;
    } else if ((HttpMethod.POST.equals(method()) || HttpMethod.PUT.equals(method()))
            && (headers().contains(HttpHeaderNames.CONTENT_TYPE) == false
                    || headers().get(HttpHeaderNames.CONTENT_TYPE)
                            .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString()))) {
        ByteBuf content = Unpooled.copiedBuffer(convertParametersToString(), CharsetUtil.UTF_8);

        headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
        content().clear();//w ww  . j  a va2s  .  c  o  m
        content().writeBytes(content);
    }

    setUri(address);
}

From source file:reactor.ipc.netty.http.client.HttpClientFormEncoder.java

License:Open Source License

/**
 * Finalize the request by preparing the Header in the request and returns the request
 * ready to be sent.<br> Once finalized, no data must be added.<br> If the request
 * does not need chunk (isChunked() == false), this request is the only object to send
 * to the remote server./*from w ww  .j  a  va2 s  . c om*/
 *
 * @return the request object (chunked or not according to size of body)
 *
 * @throws ErrorDataEncoderException if the encoding is in error or if the finalize
 *                                   were already done
 */
HttpRequest finalizeRequest() throws ErrorDataEncoderException {
    // Finalize the multipartHttpDatas
    if (!headerFinalized) {
        if (isMultipart) {
            InternalAttribute internal = new InternalAttribute(charset);
            if (duringMixedMode) {
                internal.addValue("\r\n--" + multipartMixedBoundary + "--");
            }
            internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
            multipartHttpDatas.add(internal);
            multipartMixedBoundary = null;
            currentFileUpload = null;
            duringMixedMode = false;
            globalBodySize += internal.size();
        }
        headerFinalized = true;
    } else {
        throw new ErrorDataEncoderException("Header already encoded");
    }

    HttpHeaders headers = request.headers();
    List<String> contentTypes = headers.getAll(HttpHeaderNames.CONTENT_TYPE);
    List<String> transferEncoding = headers.getAll(HttpHeaderNames.TRANSFER_ENCODING);
    if (contentTypes != null) {
        headers.remove(HttpHeaderNames.CONTENT_TYPE);
        for (String contentType : contentTypes) {
            // "multipart/form-data; boundary=--89421926422648"
            String lowercased = contentType.toLowerCase();
            if (lowercased.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString())
                    || lowercased.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
                // ignore
            } else {
                headers.add(HttpHeaderNames.CONTENT_TYPE, contentType);
            }
        }
    }
    if (isMultipart) {
        String value = HttpHeaderValues.MULTIPART_FORM_DATA + "; " + HttpHeaderValues.BOUNDARY + '='
                + multipartDataBoundary;
        headers.add(HttpHeaderNames.CONTENT_TYPE, value);
    } else {
        // Not multipart
        headers.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
    }
    // Now consider size for chunk or not
    long realSize = globalBodySize;
    if (isMultipart) {
        iterator = multipartHttpDatas.listIterator();
    } else {
        realSize -= 1; // last '&' removed
        iterator = multipartHttpDatas.listIterator();
    }
    headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize));
    if (realSize > chunkSize || isMultipart) {
        isChunked = true;
        if (transferEncoding != null) {
            headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
            for (CharSequence v : transferEncoding) {
                if (HttpHeaderValues.CHUNKED.contentEqualsIgnoreCase(v)) {
                    // ignore
                } else {
                    headers.add(HttpHeaderNames.TRANSFER_ENCODING, v);
                }
            }
        }
        HttpUtil.setTransferEncodingChunked(request, true);

        // wrap to hide the possible content
        return new WrappedHttpRequest(request);
    } else {
        // get the only one body and set it to the request
        HttpContent chunk = nextChunk();
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullRequest = (FullHttpRequest) request;
            ByteBuf chunkContent = chunk.content();
            if (fullRequest.content() != chunkContent) {
                fullRequest.content().clear().writeBytes(chunkContent);
                chunkContent.release();
            }
            return fullRequest;
        } else {
            return new WrappedFullHttpRequest(request, chunk);
        }
    }
}