Test my Spring Boot endpoints with curl

I'm learning Spring Boot and I want to test my endpoints using curl, but I have a problem: I can't because returns me 401 code, possibly I need a basic authentication (curl username and password). How can set it up in curl and in my Spring Boot project?
11 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Franscis123$#! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd6mo ago
do you already have authentication set up in spring or you dont have any at all
Franscis123$#
Franscis123$#OP6mo ago
No. I don't have any authentication.
ayylmao123xdd
ayylmao123xdd6mo ago
also a better option would be to use postman btw looks way better anyway is your application on your local machine show the endpoint and the response
Franscis123$#
Franscis123$#OP6mo ago
Yeah, is in local.
package com.latteIceCream.latte.controllers;

import com.latteIceCream.latte.domain.Flavor;
import com.latteIceCream.latte.service.FlavorService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/flavors")
public class FlavorController

{

private final Logger logger = LoggerFactory.getLogger(FlavorController.class);

@Autowired
FlavorService flavorService;

@PostMapping
public ResponseEntity<Flavor> postFlavor(@RequestBody Flavor flavor)

{

logger.info("Received request to create flavor: {}", flavor.getName());

Flavor createdFlavor = flavorService.createFlavor(flavor);

if(createdFlavor == null){ return ResponseEntity.badRequest().build(); }

return new ResponseEntity<>(createdFlavor, HttpStatus.CREATED);

}

@GetMapping("/{name}")
public ResponseEntity<Flavor> getFlavor(@PathVariable String name)

{

Flavor flavor = flavorService.readFlavor(name);

if(flavor == null){ return ResponseEntity.notFound().build(); }

return ResponseEntity.ok(flavor);

}

}
package com.latteIceCream.latte.controllers;

import com.latteIceCream.latte.domain.Flavor;
import com.latteIceCream.latte.service.FlavorService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/flavors")
public class FlavorController

{

private final Logger logger = LoggerFactory.getLogger(FlavorController.class);

@Autowired
FlavorService flavorService;

@PostMapping
public ResponseEntity<Flavor> postFlavor(@RequestBody Flavor flavor)

{

logger.info("Received request to create flavor: {}", flavor.getName());

Flavor createdFlavor = flavorService.createFlavor(flavor);

if(createdFlavor == null){ return ResponseEntity.badRequest().build(); }

return new ResponseEntity<>(createdFlavor, HttpStatus.CREATED);

}

@GetMapping("/{name}")
public ResponseEntity<Flavor> getFlavor(@PathVariable String name)

{

Flavor flavor = flavorService.readFlavor(name);

if(flavor == null){ return ResponseEntity.notFound().build(); }

return ResponseEntity.ok(flavor);

}

}
curl -v -X POST http://localhost:8080/flavors -H "Content-Type: application/json" -d '{"name": "Chocolate"}'
curl -v -X POST http://localhost:8080/flavors -H "Content-Type: application/json" -d '{"name": "Chocolate"}'
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
* using HTTP/1.x
> POST /flavors HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.14.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 21
>
* upload completely sent off: 21 bytes
< HTTP/1.1 401
< Set-Cookie: JSESSIONID=21C26F8C8532D5C0A7CD3B1CE776308D; Path=/; HttpOnly
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 0
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< WWW-Authenticate: Basic realm="Realm"
< Content-Length: 0
< Date: Thu, 12 Jun 2025 13:33:34 GMT
<
* Connection #0 to host localhost left intact
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
* using HTTP/1.x
> POST /flavors HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.14.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 21
>
* upload completely sent off: 21 bytes
< HTTP/1.1 401
< Set-Cookie: JSESSIONID=21C26F8C8532D5C0A7CD3B1CE776308D; Path=/; HttpOnly
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 0
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< WWW-Authenticate: Basic realm="Realm"
< Content-Length: 0
< Date: Thu, 12 Jun 2025 13:33:34 GMT
<
* Connection #0 to host localhost left intact
ayylmao123xdd
ayylmao123xdd6mo ago
do you have spring security added i dont mean any code just the dependency
Franscis123$#
Franscis123$#OP6mo ago
Yeah
ayylmao123xdd
ayylmao123xdd6mo ago
if you remove the dependency its gonna work but if you want to keep the dependency you need to setup the endpoints sooooo if you choose the latter make a class named SecurityConfig and paste this
@Configuration
@EnableWebSecurity
public class SecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.anyRequest().permitAll())
.httpBasic(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable);
return http.build();
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}

}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.anyRequest().permitAll())
.httpBasic(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable);
return http.build();
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}

}
and then when you launch the application it will work that might or might not work depending on your spring security version but for the new ones will work
Franscis123$#
Franscis123$#OP6mo ago
Thanks! Now it works and create the register in the DB as expected
JavaBot
JavaBot6mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot6mo ago
Post Closed
This post has been closed by <@1117467821270650921>.

Did you find this page helpful?