Cara Ngirim Data Formulir Kanthi Panyuwunan Kiriman sing Pesthekake

Cara ngirim panjalukan POST kanthi Jaminan. Formulir HTML nggunakake panjalukan POST kanggo ngirim data formulir lan ing tutorial iki, kita nggunakake Jaminan kanggo ngirim formulir.

Panjaluk POST duwe papat unsur:

URL : Iki minangka lokasi sumber sing dikirim data, kayata. www.example.com/login


KECIL : nalika ngirim data, kita nggunakake panjaluk POST.

PEMIMPIN : iki minangka header panjaluk, kayata Tampa utawa Jenis Konten.


BADAN : body ngemot data sing kita kirimake minangka panjaluk kiriman. Contone, nalika ngirim formulir, data formulir bakal dikirim ing awak panjaluk.





Panyuwunan POST sing dijamin

Kode conto ing ngisor iki nuduhake cara ngirim data formulir minangka panjaluk POST kanthi yakin, versi 3.2.0 .


io.rest-assured
rest-assured
3.2.0
test
import io.restassured.RestAssured; import io.restassured.http.ContentType; import org.junit.Test; import static io.restassured.RestAssured.given; public class restAssuredPostRequest {
@Test
public void submitForm() {
RestAssured.baseURI = 'https://www.example.com';
given().urlEncodingEnabled(true)

.param('username', 'user@site.com')

.param('password', 'Pas54321')

.header('Accept', ContentType.JSON.getAcceptHeader())

.post('/login')

.then().statusCode(200);
} }

Gegandhengan:



Payload POST JSON sing paling apik

Saliyane ngirim data Formulir, sampeyan uga bisa nggunakake panjalukan POST sing wis dijamin REST kanggo ngirim muatan JSON menyang sawetara sumber. Mangkene conto:


import io.restassured.http.ContentType; import io.restassured.response.Response; import static io.restassured.RestAssured.given; public class PostJsonPayload {
private static String payload = '{ ' +
' 'description': 'Some Description', ' +
' 'id': 'Some id', ' +
' 'name': 'Some name' ' +
'}';

public static Response postJsonPayload() {
return

given()

.contentType(ContentType.JSON)

.body(payload)

.post('/some/resource')

.then()

.statusCode(200)

.extract()

.response();
} }