JumpKick.HttpLib — POST‑Anfragen
1. Übersicht verfügbarer Anfragemethoden (statische‑Klasse Http)
// Alle Methoden geben ein RequestBuilder‑Fluent‑Objekt zurück
Http.Get(url);
Http.Post(url);
Http.Put(url);
Http.Delete(url);
Http.Patch(url);
Http.Head(url);Code-Sprache: JavaScript (javascript)
Einheitliche Fluent‑Syntax:Anfragemethode().ParameterKonfiguration().OnSuccess().OnFail().Go();
2. Zwei Parameter‑Übergabe‑Modi für POST
.Form():Formular‑VersandContent‑Type: application/x‑www‑form‑urlencoded(Web‑Formular‑Format).Body():Rohdaten‑Payload für benutzerdefinierte JSON / XML / SOAP‑Nachrichten
3. Beispiel 1:Form‑Formular‑Absenden
using System;
using JumpKick.HttpLib;
class Program
{
static void Main(string[] args)
{
Http.Post("https://httpbin.org/post")
.Form(new
{
name = "James",
username = "j6mes"
})
.OnSuccess(result =>
{
Console.WriteLine("==== POST‑Form Anfrage erfolgreich ====");
Console.WriteLine(result);
})
.OnFail(webexception =>
{
Console.WriteLine("==== Anfrage‑Ausnahme ====");
Console.WriteLine(webexception.Message);
})
.Go();
Console.ReadKey();
}
}Code-Sprache: JavaScript (javascript)
4. Beispiel 2:JSON‑Daten über Body übermitteln
using System;
using JumpKick.HttpLib;
class Program
{
static void Main(string[] args)
{
string jsonStr = "{\"account\":\"test\",\"pwd\":\"123456\"}";
Http.Post("https://httpbin.org/post")
.Body(jsonStr, "application/json") // Payload‑Inhalt + Angabe des MIME‑Typs
.OnSuccess(result =>
{
Console.WriteLine("==== POST‑JSON Anfrage erfolgreich ====");
Console.WriteLine(result);
})
.OnFail(webexception =>
{
Console.WriteLine("==== Anfrage‑Ausnahme ====");
Console.WriteLine(webexception.Message);
})
.Go();
Console.ReadKey();
}
}Code-Sprache: JavaScript (javascript)
5. Beispiel 3:Fehlgeschlagene POST‑Anfrage behandeln
Ziel ist absichtlich ein Endpunkt, derPOST‑Anfragen ablehnt,um Fehler 405 abzufangen und OnFail zu prüfen
using System;
using JumpKick.HttpLib;
class Program
{
static void Main(string[] args)
{
// httpbin.org/get akzeptiert nur GET; POST liefert Status‑Code 405
Http.Post("https://httpbin.org/get")
.Form(new
{
test = "demo"
})
.OnSuccess(result =>
{
Console.WriteLine("==== Anfrage erfolgreich ====");
Console.WriteLine(result);
})
.OnFail(webexception =>
{
Console.WriteLine("==== Anfrage‑Ausnahme (405 abgefangen) ====");
Console.WriteLine(webexception.Message);
})
.Go();
Console.ReadKey();
}
}Code-Sprache: JavaScript (javascript)
6. Häufige Fehlererklärung
Fehler:Der Remoteserver hat einen Fehler zurückgegeben: (405) Methode nicht erlaubt.
Ursache:Die Ziel‑API akzeptiertnur GET und lehnt POST ab.
Lösungsansätze:
- Wechsel zu einem POST‑fähigen Test‑Endpunkt (empfohlen: httpbin.org/post)
- Prüfen der vom API erlaubten HTTP‑Methoden
7. GET / POST Vergleichszusammenfassung
| Methode | Funktion | Parameter‑Übergabe | Einsatzbereich |
|---|---|---|---|
| GET | Http.Get() | URL‑Query‑Parameter | Datenabfrage |
| POST Formular | Http.Post().Form() | Schlüssel‑Wert‑Paare aus Formular | Web‑Login, Formular‑Versand |
| POST JSON | Http.Post().Body() | Roh‑String‑Payload | Frontend‑Backend‑API‑Kommunikation |
POST‑Anfragen
Previous: GET‑Anfragen
Next: Datei‑Upload Upload